Questions tagged [writeablebitmap]

Bitmaps are generally implemented as immutable objects in WPF. What this means is that once you create a bitmap you can't make any changes to it. You can manipulate bitmaps by creating new versions, which then immediately become immutable and sometimes this is a good way to work. Immutable bitmaps can be very efficient unless you want to indulge in a lot of dynamic changes in which case the overhead of creating and destroying them rapidly becomes too expensive. In this situation you need something a little more flexible - the WriteableBitmap. The WriteableBitmap, as its name suggests, isn't immutable and you can get at its individual pixels and manipulate them as much as you want. This is the ideal way to work when you need dynamic bitmaps. So let’s take a look at WriteableBitmap, how it works and how to use it to do dynamic things. Notice that the WriteableBitmap class in Silverlight is very different to the same class in WPF. To use WriteableBitmap we need to add:

using System.Windows.Media.Imaging;

which contains all of the additional bitmap facilities we need. You can create a WriteableBitmap in two ways. The most commonly used is to simply specify the size and format of the bitmap:

 WriteableBitmap wbmap = new 
      WriteableBitmap(100, 100, 300,
       300, PixelFormats.Bgra32, null);

This specifies a WriteableBitmap 100 by 100 pixels with a resolution of 300dpi by 300 dpi using a Bgra32 pixel format. Each pixel, a 32-bit int, uses a four-byte BGRA – that is the pixel is made up of a byte giving the Blue, Green, Red and Alpha values. The final parameter is used to specify a palette if the format needs one. You can specify a wide range of formats for the bitmap you create and in each case each pixel takes a given number of bits to represent and you need to find out how the bits allocated to each pixel determine its colour. The second method of creating a WriteableBitmap is to base it on an existing BitmapSource or derived class. For example, you should be able to create a WriteableBitmap from a standard bitmap using the constructor: WriteableBitmap(bitmapsource); but if you try this with a BitmapImage loaded from a URI you will get a null object error. The reason is that the bitmap might not yet be downloaded. For a local file the bitmap load blocks execution until it is loaded:

 Uri uri = new Uri(@"pack://application:
       ,,,/Resources/mypic.jpg");
 BitmapImage bmi = new BitmapImage(uri);
 WriteableBitmap bmi2 = new
            WriteableBitmap(bmi);
 image1.Source = bmi2;

In this case the WriteableBitmap is created with no problems. If the URI was an HTTP URL, however, the load would not block the execution and the result would be an error.

280 questions
1
vote
1 answer

How to Resolve WriteableBitmap AccessViolationException

I'm currently using the WriteableBitmapEx extension for my writeable Bitmap class. So far everything works now I'm doing this in a WinRT project. I run this code to merge the bitmaps together. Windows.Storage.Pickers.FileSavePicker save = new…
1
vote
1 answer

WP8 WriteableBitmap constructor keeps a lot of memory

I am trying to use the WriteableBitmap object because I need it for rotating images and saving images to the Isolated Storage of my app. The problem is, it uses so much memory it eventually causes an out of memory exception. Here is a picture of…
1
vote
1 answer

Select an image from rectangle using writeablebitmap

I have written an application in silverlight, I am placing a rectangle on the image and want to select the part of image covered by rectangle and show it on a image control on click of a button. I am not good at handling ratios and image…
1
vote
1 answer

Writeablebitmap reading in an asynchronous call

I want to pass a writeablebitmap object generated from UI (WPF) into an asynchronous call but don't want to update the main thread's writeablebitmap. I just want to read some properties such as pixel height and width. In the new thread I can't…
Jack B
  • 139
  • 7
1
vote
2 answers

When using a DirectX-based API with WPF (i.e. SlimDX, SharpDX, etc.) can you do sub-window-sized controls?

In our WPF application, we have a need to display about 64 real-time level meters for an audio application. The tests we've thrown at WPF, even when rendering basic primitives as efficiently as we can still show it to be nowhere near where our…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
1
vote
4 answers

How can you copy part of a writeablebitmap to another writeablebitmap?

How would you copy a part from one WriteableBitmap to another WriteableBitmap? I've written and used dozens of 'copypixel' and transparent copies in the past, but I can't seem to find the equivalent for WPF C#. This is either the most difficult…
zetar
  • 1,225
  • 2
  • 20
  • 45
1
vote
1 answer

WPF window image updating from menuitem but not when in while loop

Okay, this is a real head-scratcher: If I select a menuitem that causes the image, that makes up the entire window (a writeableBitmap) to have some pixels drawn on it, it does so and displays correctly. However, if I add a while loop (let's say for…
zetar
  • 1,225
  • 2
  • 20
  • 45
1
vote
1 answer

WriteableBitmap crashes program with no message?

I create a WriteableBitmap object, draw a line and try to set it as the source to an Image control. For some reason, the program stops responding and then closes 5 seconds later when I try to set the Source. Anyone have any idea what's wrong? (I am…
Oztaco
  • 3,399
  • 11
  • 45
  • 84
1
vote
1 answer

System.IndexOutOfRangeException when calling GetPixel() on a WriteableBitmap

private void selectColor_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { xMove += e.DeltaManipulation.Translation.X; yMove += e.DeltaManipulation.Translation.Y; double xMax = 350; double yMax =…
r wank
  • 352
  • 7
  • 22
1
vote
1 answer

silverlight 5: force grid being in sync before screen-capture via writeablebitmap

In silverlight 5: I have built a product configurator that also constructs a drawing via combination of image's and Rectangle,Ellipse,Polygon constructions. The configurator works fine and the drawing is fine as well. When a product is ready it's…
Ton
  • 23
  • 4
1
vote
1 answer

WriteableBitmapEx in console application?

I was wondering if it's possible to use the WriteableBitmap class / the WriteableBitmapEx framework in an ordinary C# console application? I have tried to add it via nuget and also included a using statement for System.Windows.Media.Imaging, but the…
Thomas
  • 4,030
  • 4
  • 40
  • 79
1
vote
1 answer

Rectifiy image of DVD cover

I'm trying to get the cover image of a DVD as a rectangular image from a photo. Now as the cover on the photo is usually a bit skewed I need to rectify it programmatically. I've already removed most of the surroundings using the Laplacian of Gauss…
1
vote
1 answer

Manipulate WriteableBitmap Pixels

I'm trying to merge two images I have as WriteableBitmaps. I want to square each pixel value, add it to the other image and then take the 2nd root (+ check, if the value is above 255). As I had no luck doing so using WriteableBitmapEx and ForEach()…
Thomas
  • 4,030
  • 4
  • 40
  • 79
1
vote
1 answer

X,Y coordinates for WriteableBitmap and GestureListener_Tap do not match - Windows Phone 8

I am creating a hidden object game and am trying to mark the object when found with an eclipse. I've manually saved the top left and bottom right coordinates of each picture which were obtained via the GestureListener_Tap event. The problem is when…
PutraKg
  • 2,226
  • 3
  • 31
  • 60
1
vote
1 answer

share writeablebitmap always failed

I want to share a writeablebitmap, but when I open the share target app, there's no image. Below is my code. private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) { DataPackage requestData =…
James
  • 2,570
  • 7
  • 34
  • 57