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

WPF WriteableBitmap Memory Leak?

I'm trying to figure out how to release a WriteableBitmap memory. In the next section of code I fill the backbuffer of a WriteableBitmap with a really large amount of data from "BigImage" (3600 * 4800 px, just for testing) If I comment the lines…
Mario
  • 319
  • 5
  • 12
6
votes
1 answer

Can I use Silverlight's WriteableBitmap to save non-visible parts of my UI to a bitmap?

Say I have some grid that you need to scroll down to see all of its lines, and I'm interested in saving some lines that are not currently visible as a bitmap. Is it feasible, or do I have to actually scroll down, "take a snapshot", and then scroll…
r0u1i
  • 3,526
  • 6
  • 28
  • 36
6
votes
1 answer

Drawing on WriteableBitmap

I have a WriteableBitmap and would like the user to be able to draw over it as if it was an InkPresenter control. What is the best way to go about doing this in realtime? Using WriteableBitmap.Pixels, I'm able to access each pixel, but when I try to…
Brap
  • 2,647
  • 2
  • 19
  • 15
6
votes
0 answers

RenderTargetBitmap is always anti-aliasing my DrawingVisual

I have to render the text from a TextBox into a WriteableBitmap. This code is working pretty well but the text in it is blurry or anti-aliased. Any ideas on how to keep it aliased and crisp? text = new FormattedText(tb.Text, new…
6
votes
1 answer

Performance of WriteableBitmap(Ex) vs DrawingContext

I am building a custom UI framework in WPF where I basically ditch as much of the built-in layout system and controls as I can. So far I have been branching off from UIElement directly, but since I am already doing the measure, arrange and rendering…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
6
votes
1 answer

Converting WriteableBitmap to Bitmap in C#

Is there any way for converting WriteableBitmap to Bitmap in C# ?
GVillani82
  • 17,196
  • 30
  • 105
  • 172
5
votes
0 answers

Anti-Aliased line algorithm with thickness (on WriteableBitmap)

I am looking for a code sample for an anti-aliased line drawing algorithm (e.g. Wu, Bresenham) that handles thickness. This is for use in a cross platform application using WPF/Silverlight's WriteableBitmap, however any C-based language will no…
5
votes
1 answer

Pixel by Pixel Color Conversion WriteableBitmap => PNG Black only to Color with Transparency

I am working on a silverlight application, where all my icons are PNGs. The color of all these icons is black, or rather black to gray, depending on the alpha value. Every PNG has a transparent background. Inside my application, I want to do a…
Michael
  • 938
  • 1
  • 10
  • 34
5
votes
1 answer

load image into writeablebitmap

So Im trying trying to load an image into a WriteableBitmap but I'm getting a NullReferenceException. Can't figure out why because I think this used to work before BitmapImage b = new BitmapImage(new Uri(@"images/Car.jpg",…
kiznore
  • 159
  • 2
  • 4
  • 13
5
votes
2 answers

Convert ImageSource to WriteableBitmap in Metro Windows 8

I was wondering how I could go about converting an ImageSource object (in my case the source property of an image element) to a WriteableBitmap. The WriteableBitmap(BitmapSource) constructor doesn't work in Windows 8 Metro, so I can't do that. I've…
5
votes
3 answers

Using FJCore to encode Silverlight WriteableBitmap

I am trying to find out how to use FJCore to encode a WriteableBitmap to a jpeg. I understand that WriteableBitmap provides the raw pixels but I am not sure how to convert it to the format that FJCore expects for its JpegEncoder method. JpegEncoder…
RHLopez
  • 211
  • 1
  • 3
  • 10
5
votes
1 answer

How can I merge 2 images on Windows Phone

I have 2 images and I want to merge them into one on my apps in Windows Phone. The first image captured by my WP's camera, the second image is a frame (borders, filter, etc.) which user can choose among our templates. So how can I merge them into…
Cong Tran
  • 1,448
  • 14
  • 30
4
votes
3 answers

Opening an image file into WritableBitmap

Here is the problem. I want to open a file from local drives, then make it into a WritableBitmap so i can edit it. But the problem is, i cannot create a WritableBitmap from Uri or something like that. Also i know how to open a file into BitmapImage…
gkaykck
  • 2,347
  • 10
  • 35
  • 52
4
votes
3 answers

Does Silverlight 4's WriteableBitmap *really* cripple legit cross-domain access in the name of DRM?

In this thread from a year ago it's explained that WriteableBitmap will block read access when any part of it comes from an outside domain - say a free image server. It's further elaborated upon that this is for "DRM". I guess there's some big…
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
4
votes
1 answer

System.Windows.Media.Imaging.WriteableBitmap leaking memory?

Consider the following code throws an exception 'MILERR_WIN32ERROR': while(true) { System.Windows.Media.Imaging.WriteableBitmap writableBitMap = new…
meds
  • 21,699
  • 37
  • 163
  • 314
1
2
3
18 19