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
2 answers

Null Reference Pointer error at WriteableBitmap when trying to save image in WP7

Not sure why, and two days of trying different things I'm getting nowhere. Keep getting the NRP at the WriteableBitmap line. You can see I've tried to both close and flush (and both together) the stream. Any ideas would be appreciated. …
0
votes
1 answer

save greyscale image as color JPEG

I'm handling a lot of different image formats within my application. Luckily I was able to source out the biggest part of dealing with image formats to C#WPF. However, I've got an issue when saving images: I'd like to save my images as JPEGs using…
0
votes
1 answer

JPG corrupted on upload in silverlight

After an image is taken with CameraCaptureTask it should be uploaded to server. The uploaded JPG on server side seems to have correct file size but is corrupted. Also imageBuffer seems to have all bytes set to 0. Any idea of what is wrong with the…
0
votes
0 answers

How to send a WriteableBitmap over ComPort ?

We are updating the WriteableBitmap every 30 ms. private void dt_Tick(object sender, EventArgs e) { wbm.Clear(); wbm.Blit(r, wbr, r); wbm.Blit(r, wbl, r); } now i have to send it over a virtual com port. this…
Aj_1994
  • 81
  • 3
0
votes
1 answer

How to scale an image to half size through an array of bytes?

I found many examples about how to scale an image in Windows Forms, but at this case I'm using an array of bytes in a Windows Store application. This is the snippet code what I'm using. // Now that you have the raw bytes, create a Image…
0
votes
1 answer

WriteAbleBitmap.Pixels[] returns the wrong color (offset?)

I'm trying to get the pixel color of a specific position (Tap event). I have the following code: private void iiiimage_Tap(object sender, System.Windows.Input.GestureEventArgs e) { Point point = e.GetPosition(iiiimage); …
Niels
  • 2,496
  • 5
  • 24
  • 38
0
votes
0 answers

Does the WriteableBitmap initialization start some background thread?

I don't know if I need to give more information but I have been browsing through my code and I couldn't find anything else that causes this problem. I have a WriteableBitmap somewhere inside my code that I only declare via WriteableBitmap wb; Now…
philkark
  • 2,417
  • 7
  • 38
  • 59
0
votes
1 answer

C#: 3D Bitmap transformation in Windows Store App

In my Windows Store App, Id like to do a 3D transformation to an image and blit it onto another image. The image should be transformed in a way so that the four corners end up on four predefined coordinates in the second image. Example: A user can…
Thomas
  • 4,030
  • 4
  • 40
  • 79
0
votes
1 answer

Sketch effect on an image in windows phone 7

I am working on an image editor app having functions like cropping, text rendering, framing etc. I want to implement sketch effect now ie, the photo shall be converted to sketch. I am using Nokia.Graphics.Imaging.Managed library for doing it. It…
A.K.
  • 3,321
  • 1
  • 15
  • 27
0
votes
1 answer

Resizing an Image using WriteableBitmap is not working

I am using the Windows Phone CameraCaptureTask to gather an image, and then resize it for better use within my application. For some reason, no matter what I've tried I cannot get the image to resize. MainPage.xaml.cs private void…
Matthew
  • 3,976
  • 15
  • 66
  • 130
0
votes
0 answers

Userdefined BitmapSource with WriteableBitmap

I'm having a DICOM image data, which holds 16bit gray values. To visualize the data I need to apply a windowing method that cuts out pieces of the 16bits values. Therefore I have two properties, like WindowCenter and WindowWidth. Since I want to use…
msedi
  • 1,437
  • 15
  • 25
0
votes
0 answers

Multiple bitmap blits too fast for system?

I've got a an image (inside of a scroll viewer) that is 2,000 x 2,000 pixels. In a while loop I'm calling the following code 5 times: writeableBitmap = new WriteableBitmap(CleanVegMap); image.Source = writeableBitmap; DrawDinos2d(); What is…
zetar
  • 1,225
  • 2
  • 20
  • 45
0
votes
1 answer

WinRT UI-elements rendering

There is a need to make the picture from a UI element (WriteableBitmap or something similar). At Windows Phone this problem is solving by this way: var frameworkElement = new Button(); ..... var wrb = new WriteableBitmap(frameworkElement,…
0
votes
1 answer

Share WriteableBitmap as File

I'm currently writing a small image editing app for the Windows Store in which I'd like to make use of the share charm for edited images. As some apps do only accept StorageFiles and not Bitmaps (like the default Mail App) I'd like to offer the…