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 catch color fault in WriteableBitmapEx GetColor WP7

I just read any image and could not get the correct colors. I've tried several images with several formats. Please advice how to receive the correct colors in WriteableBitmap.GetPixels() In the following example, I just cloned the image by reading…
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
1
vote
0 answers

Silverlight WriteableBitmap not converting custom font correctly

We use custom fonts to represent road signs and shapes that we plot on a graph. Initially I plotted everything to a canvas, which worked fine, but was slow to scroll horizontally. Then I fixed the scrolling issue by converting the canvas to a image…
Martin Lottering
  • 1,624
  • 19
  • 31
1
vote
2 answers

Silverlight: converting a byte[] to a BitmapImage ends in "catastrophic failure"

I am trying to modify a BitmapImage in Silverlight, but happen to run into a "catastrophic failure"! First I load an image and add it to the LayoutRoot. BitmapImage source = new BitmapImage(new Uri("image.jpg", UriKind.Relative)); Image…
1
vote
2 answers

WriteableBitmap PixelBuffer Stream Length Too Small

I'm running into an issue where I'm trying to copy the pixel buffer for one WriteableBitmap over to another WriteableBitmap essentially giving a copy of the WriteableBitmap object. However, when I try to do this I run into an issue where the second…
GGCO
  • 183
  • 5
  • 12
1
vote
2 answers

C# convert stride/buffer/width/height to bitmap

I have an image width/height/stride and buffer. How do I convert this information to a System.Drawing.Bitmap? Can I get the original image back if I have these 4 things?
Mattb2291
  • 167
  • 1
  • 3
  • 8
1
vote
2 answers

Erosion and Dilation in Windows phone???

i trying to apply the Erosion and Dilation algorithms in my app for Windows Phone :). But is very hard to do :( i investigated for some example and i found this…
1
vote
1 answer

Take a snapshot of using the rectangle in WP7

Im working on a WP7-app where I would like to record video and before the video is saved take a snapshot of the video so that it could be used as a thumbnail picture. The thumbnail picture is temporary save in the isolated storage before it is used.…
user473104
  • 301
  • 2
  • 6
  • 17
0
votes
1 answer

How I can use getPixel method on BitmapImage class?

I mean I want to use get pixel method on wp7. I do not use Bitmap class.It is possible or I don't use it and I can use writeableBitmap instead of Bitmap. And when I use writeableBitmap class I can not use getPixel method.How I can use? for…
CompEng
  • 7,161
  • 16
  • 68
  • 122
0
votes
1 answer

Silverlight 4 Render to WriteableBitmap returns empty bitmap

I have some code that aims to render a ControlTemplate to a writeableBitmap. The code to create the WBMP is as follows: private static void OnMarkerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var…
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
0
votes
1 answer

WriteableBitmap SetValue performance / Texture2D SetData

I was just wondering if anyone knew (performance wise on wp7) the best way to update a WriteableBitmap pixel by pixel... I can see the Pixels.SetValue method which can take in a colour and a single location. But surely a faster option is to set an…
Wacka
  • 87
  • 1
  • 9
0
votes
3 answers

Silverlight - Passing WritableBitmap Image to COM and convert to Bitmap

Need help in passing Image from Silverlight4 to COM. I am trying to pass a ByteArray from WritableBitmap and convert it back to Bitmap. //In silverlight 4: public string func1() { WriteableBitmap bitmap = new…
Kaps
  • 831
  • 7
  • 8
0
votes
1 answer

WriteableBitmap Transform to Center?

I need to put an user control (a TextBlock in this case) in the horizontal center of a WriteableBitmap, here is the code that i come with so far: textblock1.RenderTransformOrigin = new Point(0.5, 0.5); wp.Render(textblock1, new TranslateTransform()…
blacker
  • 37
  • 1
  • 6
0
votes
1 answer

WriteableBitmap blending oddly with background grid

I'm experimenting with drawing to a WriteableBitmap assigned to an Image control within WPF and I am finding that the color values are blending with the background color even when the alpha values are set to 0. I have the following main…
0
votes
1 answer

How to eliminate artifacting with fast update WriteableBitmap

I'm trying to implement a video renderer with a WriteableBitmap. My current implementation works fine for full screen 60fps content, but if I scrub the video (drag the playback back/forth to a new time) the renderer starts updating the…
Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
0
votes
0 answers

Why does this happen to the image when i use writablebitmap is it reading it wrong? do i need to have an offset?

I have been trying to load an image (which works) as a bitmapimage then convert that writeablebitmap to grey scale by averaging all of the colours together how ever when ever i do it the image has green or blue and red line across seemly dependent…