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
20
votes
5 answers

WriteableBitmap Memory Leak?

i am using the code below to create a live tile, based on an UI element. It renders the uiElement on a WriteableBitmap, saves the bitmap + returns the filename. This method is run in a windows phone background task agent an i am running into memory…
Hannes
  • 213
  • 2
  • 8
19
votes
6 answers

Calculating the required buffer size for the WriteableBitmap.WritePixels method

How do I calculate the required buffer size for the WriteableBitmap.WritePixels method? I am using the overload taking four parameters, the first is an Int32Rect, the next is a byte array containing the RGBA numbers for the colour, the third is the…
JMK
  • 27,273
  • 52
  • 163
  • 280
14
votes
6 answers

How can I render text on a WriteableBitmap on a background thread, in Windows Phone 7?

I am trying to render text on a bitmap in a Windows Phone 7 application. Code that looks more or less like the following would work fine when it's running on the main thread: public ImageSource RenderText(string text, double x, double y) { var…
Ran
  • 5,989
  • 1
  • 24
  • 26
14
votes
1 answer

Save WriteableBitmap to file using WPF

I have: WriteableBitmap bmp; I basicly want to save it into a file on the disk like the following: C:\bmp.png I read some forums which mentions to read: bmp.Pixels and save those pixels into a Bitmap then use Bitmap.SaveImage() function. However,…
Sait
  • 19,045
  • 18
  • 72
  • 99
12
votes
2 answers

How can I convert WriteableBitmap to BitmapImage?

BitmapImage bitmapImage = new BitmapImage(new Uri("arka_projects_as_logo.png", UriKind.Relative)); Image uiElement = new Image() { Source = bitmapImage }; ScaleTransform t = new ScaleTransform() { ScaleX = 0.2, ScaleY = 0.2 }; WriteableBitmap…
Sergey
  • 47,222
  • 25
  • 87
  • 129
10
votes
3 answers

Edit raw pixel data of WriteableBitmap?

Is it possible to directly read/write to a WriteableBitmap's pixel data? I'm currently using WriteableBitmapEx's SetPixel() but it's slow and I want to access the pixels directly without any overhead. I haven't used HTML5's canvas in a while, but…
Oztaco
  • 3,399
  • 11
  • 45
  • 84
8
votes
1 answer

Break image into tiles

Hey. I havea 480 x 800 image and would like to place this on my tilemap. I'm trying to split the image into into a grid (6 x 10) and assign each tile that specific portion of the image. Essentially, the tilemap will look like one big image since…
Skoder
  • 3,983
  • 11
  • 46
  • 73
8
votes
2 answers

Wpf: Why is WriteableBitmap getting slower?

There is a simple MSDN example about WriteableBitmap. It shows how to draw a freehand line with the cursor by just updating one pixel when the mouse is pressed and is moving over a WPF -Image Control. writeableBitmap.Lock(); (...set the…
fritz
  • 115
  • 1
  • 5
8
votes
1 answer

How to dispose a Writeable Bitmap? (WPF)

Some time ago i posted a question related to a WriteableBitmap memory leak, and though I received wonderful tips related to the problem, I still think there is a serious bug / (mistake made by me) / (Confusion) / (some other thing) here. So, here is…
Mario
  • 319
  • 5
  • 12
7
votes
1 answer

How to bitblit from RenderTargetBitmap to WriteableBitmap?

I'm rendering dozens of visuals to the RenderTargetBitmap. Each is rendered in it's own Rect. What I want to do is to copy one of these Rect areas rendered from RenderTargetBitmap instance into the same area of the WriteableBitmap...Fast copy rect…
Alexander Efimov
  • 2,685
  • 3
  • 18
  • 22
7
votes
3 answers

Why is my unsafe code block slower than my safe code?

I am attempting to write some code that will expediently process video frames. I am receiving the frames as a System.Windows.Media.Imaging.WriteableBitmap. For testing purposes, I am just applying a simple threshold filter that will process a BGRA…
Jon Comtois
  • 1,824
  • 1
  • 22
  • 29
7
votes
2 answers

Converting WriteableBitmap to Bitmap for use in EmguCV

In my code, I'm receiving WriteableBitmaps from a byte array (in turn from a Kinect) and I'd like to turn them into bitmaps for use with EmguCV. Currently this is the code I have: // Copy the pixel data from the image to a temporary…
Gaessaki
  • 836
  • 1
  • 8
  • 16
7
votes
2 answers

OutOfMemoryException @ WriteableBitmap @ background agent

I have a Windows Phone 8 App, which uses Background agent to: Get data from internet; Generate image based on a User Control that uses the data from step 1 as data source; In the User Control I have Grid & StackPanel & some Text and Image…
Max Meng
  • 147
  • 10
7
votes
3 answers

How to create WriteableBitmap from BitmapImage?

I could create WriteableBitmap from pictures in Assets. Uri imageUri1 = new Uri("ms-appx:///Assets/sample1.jpg"); WriteableBitmap writeableBmp = await new WriteableBitmap(1, 1).FromContent(imageUri1); but, I can't create WriteableBitmap from…
hiroo
  • 657
  • 2
  • 7
  • 13
7
votes
1 answer

How do I convert a WriteableBitmap object to a BitmapImage Object in WPF

How do I convert a WriteableBitmap object to a BitmapImage Object in WPF? This link covers silverlight, the process is not the same in WPF as the WriteableBitmap object does not have a SaveJpeg method. So my question is How do I convert a…
JMK
  • 27,273
  • 52
  • 163
  • 280
1
2 3
18 19