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
1 answer

How to save a grid as a bitmap Image in the local storage in windows 8

I am very new to Windows 8 application development. I am developing an application which requires to save a grid as Bitmap Image in local storage. I have tried by using writable bitmap but i didn't get the solution. Then I searched for the samples…
0
votes
1 answer

What is the equivlent WIC version for this bitmap manipulating routine?

We have some bitmap images which actually in RAW format but it uses a windows bitmap format, however if not using the camera provided lib to decode it you cannot have a good result. The following is the code that works fine: public static Bitmap…
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
0
votes
1 answer

WP8 - How can I make the background agent execution to wait before all ImageOpened events are fired to update live tile with custom images?

How can I make the background agent execution to wait before all ImageOpened() events are fired (3 in this case) in order to update secondary live tile with custom images? Edit 1: In the OnInvoke() method of the ScheduledAgent I am calling my own…
0
votes
1 answer

Create WriteableBitmap from UIControl WPF

I am currently converting a Silverlight application into WPF. In my silverlight application I have the code WriteableBitmap sceneBitmap = new WriteableBitmap(scene, new TranslateTransform() { Y = 10 }); WriteableBitmap newone =…
JKennedy
  • 18,150
  • 17
  • 114
  • 198
0
votes
1 answer

BitmapImage METRO re-use WPF code

I have written a little game using IronPython and WPF for didactic purpose and now I want to translate the project to Metro APP for test Shared Projects. The guilty code is: def LoadImage(name, sourceRect): bmp = BitmapImage() …
0
votes
4 answers

How to clear a WriteableBitmap

I have a WriteableBitmap which I would like to repeatedly clear and redraw. I have seen references to writing to the Pixels array directly, but the Pixels array is not accessible in WriteableBitmap. I have also tried the suggestion of recreating the…
David Sykes
  • 48,469
  • 17
  • 71
  • 80
0
votes
0 answers

WriteableBitmap without form

I'm trying to draw a WriteableBitmap on the screen without having to use a form. This is because I want to draw an overlay for an existing game. I've managed to do this by using a form with an Image in it where I draw the WriteableBitmap to, but a…
Tim
  • 123
  • 12
0
votes
1 answer

how to render canvas to bitmap excluding out of bounds elements (C# WinRT)

I have a simple paiting app and I'm trying to render a canvas into a bitmap using the RenderTargetBitmap.RenderAsync() method. If a child element of the canvas exceeds canvas boundaries, the rendered bitmap is bigger than the canvas area... How can…
Danilo
  • 13
  • 4
0
votes
1 answer

WriteableBitmap or BitmapImage to StorageFile in C++ Windows Store App

so i can save the image? The image can be found only in those format its not a file its just a BitmapImage. I know this way in c# any ideas how to make it in C++? private static async Task<StorageFile> SaveAsJpeg(WriteableBitmap wb) { …
Stamos
  • 3,938
  • 1
  • 22
  • 48
0
votes
0 answers

WriteableBitmap.Lock() throws AccessViolationException

Ok so I have an application that makes heavy use of WriteableBitmap via the WriteableBitmapEx open source library. Sometimes, rarely (and not sure the exact conditions to cause), calling WriteableBitmap.Lock() throws an AccessViolationException.…
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
0
votes
3 answers

C# Stream writeablebitmap among multiple threads

I have searched for a while regarding this problem. Got some solution, but none of them is solving my issue. The scenario is, I am fetching and processing a bitmap stream in background thread and after each frame is ready, I am trying to update the…
Shuvro
  • 3
  • 2
0
votes
1 answer

WriteableBitmap deadlock

I am using WriteableBitmap to implement video display of a streaming video (need to re-write it up to 30 times per sec) There are several of them on the screen and each has its own video to display. The video also needs to be decoded so I am using…
ILIA BROUDNO
  • 1,539
  • 17
  • 24
0
votes
2 answers

What is a writeable bitmap?

I understand that it can be used as an image source for wpf files but can someone take me through all of it's inputs and what they signify, as well as the importance of each component. Also I'm confused as to what a bitmap is, should it be called in…
Brian
  • 11
  • 4
0
votes
2 answers

How are writeable bitmaps used with the kinect in VS 2013 wpf applications to hold video information?

I'm looking at the colors basic sample from the Kinect developers toolkit using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading.Tasks; using System.Windows; using…
Brian
  • 11
  • 4
0
votes
1 answer

wp8 writeablebitmap doesn't render

I try to render some elements on writeable bitmap. It works when rendering textblock but not something else for example rectangle. Why so? void bm_ImageOpened(object sender, RoutedEventArgs e) { WriteableBitmap wbm = new…
peke-tsu
  • 51
  • 5