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

Saving an array of WriteableBitmap to file

Is it possible to save an array of WriteableBitmap to a file on disk as a whole, and retrieve it as a whole too?
P5music
  • 3,197
  • 2
  • 32
  • 81
0
votes
1 answer

Creating high performance animations in WPF

I'm in a situation that requires many animations with effects like transparency to be applied but when there are about 10 of them running, my application slows down to a grinding halt! :( I also, tried implementing a particle like effect using a…
Mark
  • 14,820
  • 17
  • 99
  • 159
0
votes
1 answer

Converting FileStream to WriteableBitmap to JPEG to Byte Array for SSRS

I'm trying to save an Image to SQL Server so SSRS can read it. I need to convert to WriteableBitmap (and possibly JPEG?) so I can make changes to the image size before saving. However, when I try to pull the converted image out of SQL Server, it…
a b
  • 57
  • 2
  • 8
0
votes
2 answers

Creating an object only once and updating

I'm kind new to c#. I have this code: public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch) { System.Windows.Media.PixelFormat format = System.Windows.Media.PixelFormats.Default; if…
Probst
  • 17
  • 1
  • 8
0
votes
1 answer

WPF WriteableBitmap and effects

Im trying to look into using the WPF WriteableBitmap class to allow my application to apply an opacity mask to an image. Basically I have a blue rectangle as an image, and another 100% transparent green rectangle image over the top of the blue…
Mark
  • 14,820
  • 17
  • 99
  • 159
0
votes
1 answer

Crop With Padding

I need to crop an image specifying coordinates that may exceed the image's bounds. If the coordinates are off, appropriate padding is applied. Normally: +===============+ | Source Bitmap | | +-------+ | | + Crop + | | +-------+ | | …
Erwin Alva
  • 403
  • 4
  • 17
0
votes
1 answer

Create WriteableBitmap from resource in console application

I'm trying to create a WriteableBitmap from an Image on my hard drive using WriteableBitmapEx. I copied the code from the official page after adding the nuget package: WriteableBitmap writeableBmp = new WriteableBitmap(0,…
Thomas
  • 4,030
  • 4
  • 40
  • 79
0
votes
1 answer

writeablebitmap always render image with black background

I have next code: var bmp = new WriteableBitmap((int)size.Width, (int)size.Height); bmp.Render(new Canvas(){Background = new SolidColorBrush(Colors.White)}, null); bmp.Invalidate(); return bmp; How I get colors: var backColor =…
Anton Shakalo
  • 437
  • 1
  • 6
  • 23
0
votes
2 answers
0
votes
0 answers

Drawing WriteableBitmap that changes

I have a controller class that returns some images as WriteableBitmap. I use that lib in my WPF app that should refresh the Image immediately. The problem is, nothing happens when I change its Source :/ private void Csm_OnPropertyChanged(object…
Nickon
  • 9,652
  • 12
  • 64
  • 119
0
votes
1 answer

Converting Marshal.Copy Bitmap to Silverlight Equivalent

I am having a problem converting the following code snippet for an existing component into Silverlight. Bitmap bmp = new Bitmap(width, height); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,…
0
votes
1 answer

conversion between bitmapimage and byte array in windows8

I write the code below to convert image to byte array and convert back later, but it doesn't work. Anyone can help? FileOpenPicker picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; …
James
  • 2,570
  • 7
  • 34
  • 57
0
votes
1 answer

C# Console app - Bing Maps - how to save output

I can get a URI from Bing Maps but when I try to save the resulting file (eg, a .jpg) it is empty (eg, 1 x 1 size). I've got it working fine in a WPF app with event handling but how do I do it with a console app? Here's the code that works with WPF…
0
votes
1 answer

Silverlight: Using WriteableBitmap to Catch Pixels from an External URL Image Throws an Security Exception

I have several UIElements, including an Image (with an External URL image), and then I want to catch a thumbnail of these elements, so I use WriteableBitmap to catch the pixels for each UIElement. However, when I try to catch the pixel for the Image…
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
0
votes
2 answers

Not clear unsafe code in draw pixel method

Lately I got low level graphics programming job to do (implementing raster graphics algorithms). That's why I started to look for tools (classes) which would be helpful to draw primitives (lines, rectangles, ellipses etc.) on the low level of…
patryk.beza
  • 4,876
  • 5
  • 37
  • 56
1 2 3
18
19