0

I am trying to display image using BitmapImage for some time and it worked.I have changed the image and it stopped working.

For Bitmapimage I was using this code:

                            `ms.Seek(0, SeekOrigin.Begin); // ms is memory stream
                            BitmapImage b = new BitmapImage();
                            b.SetSource(ms);
                            image.ImageSource = b;`

I have ran into piece of code where it was checking if the length of the bytes[] ==14400

 if(bytes.length == 14400)
  {
  var bmp = new WriteableBitmap(width, height);
  Buffer.BlockCopy(buffer, 0, bmp.Pixels, 0, buffer.Length);
 }

I want to know when to use WriteableBitmap and BitmapImage .

user2526236
  • 1,538
  • 2
  • 15
  • 29

1 Answers1

2

From iProgrammer:

Bitmaps are generally implemented as immutable objects. 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....

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. iProgrammer - WriteableBitmap

From MSDN:

"Use the WriteableBitmap class to update and render a bitmap on a per-frame basis..." MSDN - WriteableBitmap Class

The Examples section of the MSDN article also shows how to update a WritableBitmap image when responding to mouse events. The code in the example erases pixels of the image by setting the pixel's ARGB values to 0 when the mouse's right button is down. The code also shows how to update individual pixels in the image where the mouse's left button is down. Essentially the code shows a rudimentary pixel image editor.

The point, however, is that you can't change image data when using regular bitmap - you have to use WritableBitmap instead. You can, however, render both if you wish.

bleepzter
  • 9,607
  • 11
  • 41
  • 64
  • Bitmaps are Mutable / immutable, from your answer I see bitmaps are mutable as you create a bitmap you can't change it. – user2526236 Jan 14 '15 at 21:29