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
0
votes
1 answer

WriteableBitmap exception when having multiple threads

I create about 10 threads doing the same work - downloading image from the Internet. After the download is completed it will raise this callback function: private void DownloadImageWrapper(IRestResponse response, params object[] args) { byte[]…
0
votes
1 answer

WriteableBitmap unavailable?

I've discovered what may be the perfect answer to a question on buffered drawing, but for some reason my version of VS2008 doesn't seem to have a WriteableBitmap? I've tried including the relevent namespaces as per the documentation: using namespace…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
0
votes
1 answer

How to convert WriteableBitmap in RGB24 pixel format into a EmguCV image format?

I am trying to convert a WriteableBitmap which is having Rgb24 as a pixelFormat. I want to store the same image into a EmguCV Image having Bgr format. I have written a following code but it is not giving appropriate results. public unsafe void…
curiousMind
  • 155
  • 3
  • 10
0
votes
1 answer

WriteableBitmap.SaveJpeg renders a black image (WP7)

I'm trying to render some text and an image to a writeable bitmap to make 1 larger image, and this method has worked in other locations for creating or manipulating images, but for some reason, this instance is only creating a black image. If I…
Daniel
  • 73
  • 2
  • 10
-1
votes
1 answer

WP8.1 C# Windows Phone 8.1 Background Task WriteableBitmap Thread Error

I am trying to update the band Me Tile image from a background task and i get the following error at WriteableBitmap: System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT:…
-1
votes
1 answer

Unable to dispose byte[] in WriteableBitmap.WritePixels

In my WPF application, I need to load multiple images to display it in the UI. For that, I am using WriteableBitmap and then pass byte[] into Writeable.Writepixels(). While doing this, I am getting Out of memory exception after 5-6 execution. Image…
Sathyanarayanan
  • 129
  • 1
  • 1
  • 6
-1
votes
2 answers

Convert WriteableBitmap to BitmapImage using BmpBitmapEncoder WPF

I have a problem with Convert WriteableBitmap to BitmapImage using BmpBitmapEncoder. this is my method: public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm) { bmp = new BitmapImage(); using…
Lukii007
  • 85
  • 1
  • 3
  • 4
-1
votes
1 answer

Conversion of a Bitmap to Pixel Array for Microsoft OCR C#

I'm working with Microsoft's OCR library and am having problems converting the BitmapImage to a pixel array. I'm making this application for Windows Phone 8, and WriteableBitmap.PixelBuffer.ToArray() isn't an option so I have a static function…
Wezley
  • 413
  • 1
  • 3
  • 19
-2
votes
1 answer

How do I write text on WriteableBitmap?

Right now I'm coding a game and I need to draw text onto a WriteableBitmap; how do I do this without converting from WritableBitmap to Bitmap to WriteableBitmap? I've searched for solutions already, but all of them slow down my game or need…
-2
votes
1 answer

image WriteableBitmap image .Pixels The field, constructor or member 'Pixels' is not defined?

let image = WriteableBitmap(100, 100, 300.0, 300.0, Media.PixelFormats.Bgra32, null); let pixel = image.Pixels Error 2 The field, constructor or member 'Pixels' is not defined WriteableBitmap.Pixels Property MSDN Why not…
Art Scott
  • 193
  • 1
  • 2
  • 8
1 2 3
18
19