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
3
votes
3 answers

WPF/WinForms/GDI interop: converting a WriteableBitmap to a System.Drawing.Image?

How can I convert a WPF WriteableBitmap object to a System.Drawing.Image? My WPF client app sends bitmap data to a web service, and the web service needs to construct a System.Drawing.Image on that end. I know I can get the data of a…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
3
votes
2 answers

Drawing line using WPF WriteableBitmap.BackBuffer

Do you know any library that provides methods to draw simple shapes (lines and optionally other shapes) using WPF WriteableBitmap and ideally BackBuffer? I know that there is a WriteableBitmapEx project for silverlight but is there WPF equivalent?
adrin
  • 3,738
  • 8
  • 40
  • 60
3
votes
1 answer

Overlay WriteableBitmap with color

I'm trying to overlay a WriteableBitmap with a certain color in Silverlight. I have a black and white base image, which I'm using to create smaller WriteableBitmap images for a new composite image and I want to overlay either the black or white part…
Rajen
  • 41
  • 3
3
votes
0 answers

Camera RAW image processing with Windows Store app

I want to create a Windows Store app in C# to process to camera RAW images. It looks like I can load the RAW images using Microsoft's Windows Imaging Component (WIC), but when then I want to manipulate the images in a Windows Store app, I run into a…
3
votes
1 answer

How to work with pixels in Windows Phone/C#

Windows Phone 8 scales elements using the scale factor value so all pixels are virtual to 800x480 and values such as ActualWidth/ActualHeight return the "fake" 800x400 value. I'm displaying a WritableBitmap that is constructed dynamically on the…
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
3
votes
1 answer

Windows Phone - update live tile from background agent with custom image

I am trying to add cloud image to album cover if the cover is loaded from internet. I am trying to do this in Background Audio agent and I think I almost got it. The problem is that I have black image in tile. Few times when testing I got cover…
3
votes
0 answers

WPF WriteableBitmap moving object flickers

I have an object that moves. This object is represented by a list of points. The calculation is done in a seperate thread. To draw this point there is a window that has a WriteableBitmap attached. The drawing method looks something like this. Clear…
nero.ch
  • 43
  • 3
3
votes
2 answers

Asynchronous operations on WriteableBitmap

I'm writing an application in WPF (C#) which does long operations on a collection of Bitmaps. To keep my application responsive, I decided to use another thread to perform the operations on bitmaps and report progress on a progressbar in main UI…
Gylo
  • 61
  • 2
  • 3
3
votes
2 answers

where did PixelFormats and WriteableBitmap.Lock go in Silverlight3?

A few months ago I built some online samples like this one from Jeff Prosise that use the WriteableBitmap class in Silverlight. Revisiting them today with the latest Silverlight3 installer (3.0.40624.0), the API seems to have changed. I figured…
Eric
  • 11,392
  • 13
  • 57
  • 100
3
votes
1 answer

The fastest way to convert canvas to the writeablebitmap in WPF?

I currently have one writeablebitmap image and canvas with drawings and I want to send the images to the peer.In order to reduce the bandwidth, I would like to convert canvas to the writeablebitmap, thus I can blit both the images to a new…
Adam Aiken
  • 418
  • 7
  • 18
3
votes
3 answers

Sobel operator & Convolution with WriteableBitmapEx

So I'm using WriteableBitmapEx for an app on Windows RT. I'm trying to implement edge detection on an image using the sobel operator. I have successfully applied both kernels for x and y detection onto the image using .Convolute(), but now I'm stuck…
Thomas
  • 4,030
  • 4
  • 40
  • 79
3
votes
1 answer

Data Binding To Instance Method

I have an application with a local database (SQL CE), and I want to bind a listbox to a table (Car). However, I can't save WriteableBitmap to my local database so I decided to transform the image to an array of bytes so I need to call the method…
Julian J. Tejera
  • 1,015
  • 10
  • 17
3
votes
2 answers

Changes to WriteableBitmap pixels don't update screen

I have a WriteableBitmap in a Windows Phone 8 application that is hooked up to an Image control. I'm looping through each row of the image and painting a row of pixels at a time asynchronously and then scheduling the next row for painting. However,…
sohum
  • 3,207
  • 2
  • 39
  • 63
2
votes
2 answers

Issue with rendering WriteableBitmap from Image in Silverlight

I have a next problem, I need to convert array of bytes to WriteableBitmap with resize. I write next code. private byte[] ResizeImage(byte[] array, double maxWidth, double maxHeight) { WriteableBitmap wb = null; var stream = new…
2
votes
2 answers

Copy a System.Drawing.Bitmap into a region of WriteableBitmap

I'm currently having some problems in integrating a System.Drawing.Bitmap into a WPF WriteableBitmap. I want to copy from the Bitmap to position (X,Y) of the WriteableBitmap. The following code shows how I've tried to do this. BitmapData Data =…
Geotinc
  • 289
  • 1
  • 4
  • 21