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

UWP WriteableBitmap DPI Info

Our application has rendering logic that depends on the horizontal / vertical image resolutions in dots per inch. This used to be available as DpiX and DpiY properties in the WriteableBitmap / BitmapSource…
Erwin Alva
  • 403
  • 4
  • 17
1
vote
1 answer

Dashed line using writeablebitmap

Are there a way when using writeablebitmap with writeablebitmapEx to draw a dashed line? Without having to manually calculate the dashes and create multiple lines to create the dashing effect?
Androme
  • 2,399
  • 4
  • 43
  • 82
1
vote
0 answers

Detecting Color of Pixel Where Tapped in Windows Phone 8.1

There is an image in "Image" control. When user taps image control, the color should be extracted of that exact pixel. This is the code provided by msdn, for Windows Phone 8. In Chroma Key Demo For Windows 8 private async void…
Talha
  • 903
  • 8
  • 31
1
vote
2 answers

Windows Phone 8.1 Runtime - Image to byte[] and Vice Versa

For my application, I am trying to convert an WriteableBitmap to byte[] to store in the database, and then from byte[] to BitmapImage to display back to the user. My current methods that so far, produce no results: public byte[]…
1
vote
1 answer

How To Convert a Bitmap to a Collection of Paths in WPF (in code not XAML)

I have an occupancy grid that has 3 states - Occupied, Free, Unknown. Occupancy grid is a simple 2 dimensional array of states. The grid represents a floor plan where Occupied=Wall, Free=Open Floor, Unknown=what's behind the wall or not mapped. …
1
vote
1 answer

Overlay two images using WriteableBitmap

I have got two images, say png and jpeg and I need to overlay them. In WPF it can be done by DrawingGroup (unavailable in SL). I guess it might be done with WriteableBitmap instead. Do you have any idea how to do that? Thanks in advance! Cheers
Jamie
  • 1,092
  • 3
  • 22
  • 44
1
vote
1 answer

Draw rectangle WP8.1 async (or fast)

I'm trying to draw rectangles into a WriteableBitmap, unfortunatelly the WriteableBitmapEx that provides Fill* extensions are too slow and can be run only at the main thread. I'm looking for alternatives specific for WP8.1 and don't know the best…
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
1
vote
1 answer

Use StorageFile to get Bitmap Dimensions before using it to read image?

Okay, I'm trying to do some work analyzing heuristics of an image in a WPF app. When the user chooses the image file location, I want to open the image in the codebehind, check the colors of the image, and output them in an application. However to…
C Bauer
  • 5,003
  • 4
  • 33
  • 62
1
vote
1 answer

WriteableBitmap from Uri Windows phone 8.1

Hi I'm having a problem with Windows Phone 8.1 Universal app. I have to create a WriteableBitmap from an image i have on the phone. I have my uri Uri uri = new Uri("ms-appx;/Images/imageName.png"); but i honestly don't know how to use it to create…
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
1
vote
0 answers

Image source to writeablebitmap in windows phone 8.1

I'm working on windows phone 8.1 application. I have different pictures locally in app and displaying those pictures in list view. Now select one picture id and pass it to detail screen, where I have to save that file . My detail.Xaml screen…
Arsal
  • 343
  • 2
  • 8
  • 21
1
vote
1 answer

Rendering video using WriteableBitmap causes choppy animation

So here's my setup: Camera images coming in at 1920x1080 @ 25 FPS Writing image data to WriteableBitmap on UI thread (simple copy, no processing) Two Image controls in two different windows on two different monitors has their Source property set to…
Chris
  • 5,442
  • 17
  • 30
1
vote
1 answer

Silverlight: converting WriteableBitmap to stream

Given a WriteableBitmap, how can I save the bitmap back into an image stream (JPG or BMP)? My scenario is: Show OpenFileDialog, let user select image Load image into WriteableBitmap Resize image Send image stream to web service Step #4 is what I'm…
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
1
vote
1 answer

How to copy and process WriteableBitmap in separate thread?

I my kinect application I have main thread which is resposible for comunication between UI thread and other threads. I am no able to make a copy of WriteableBitmap generated from kinect and pass this WriteableBitmap image to separated thread with…
Dariusz Filipiak
  • 2,858
  • 5
  • 28
  • 39
1
vote
4 answers

How do I disable silverlight cross-domain content protection for WriteableBitmap

I have an Esri map in silverlight and I am trying to get a screenshot. However, I am facing cross-domain content protection for writeablebitmap, saying "Pixels are not accessible". Is there a way that I can disable this? or any other work around in…
Devphil
  • 295
  • 2
  • 10
  • 20
1
vote
0 answers

Windows phone 8 writeablebitmap get object drawn

Is there any way to get object drawn by writeablebitmapex in windows phone 8. In my case, i want to get eclipse which drawn previously to delete or move its. For now, i add eclipse by this code: int ansX1 = Convert.ToInt32(left-10); …