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

Convert YUV to RGB on DeckLink using hardware

I'm currently ingesting HD1080p video at 59.94 FPS from a camcorder via the HDMI input on the DeckLink 4K Extreme. My goal is to replicate the incoming image in a WPF UI element. To accomplish this I'm using the DeckLink SDK in a C# WPF…
2
votes
1 answer

Using gdi+ with InteropBitmap

Is it possible to use WPF's InteropBitmap together with gdi+ ? Any examples ? (I need it for 2D drawing, many small objects; I know how to use gdi+ with WriteableBitmap => (see post by Danko) , but after all I've read InteropBitmap should be…
fritz
  • 115
  • 1
  • 5
2
votes
1 answer

WPF WriteableBitmap to byte array

Is there anyway to convert a WriteableBitmap to byte array? I assign the writeablebitmap to an System.Windows.Controls.Image source too if there's a way to get it from that. I tried this but got a general GDI exception on FromHBitmap.…
shady
  • 185
  • 2
  • 15
2
votes
1 answer

WriteableBitmap renders PNG incorrectly

I am having trouble rendering PNGs that use Palette as "Color Type". Here is some simple code to reproduce the issue: private async System.Threading.Tasks.Task Fetch() { HttpClient httpClient = new HttpClient(); Uri uri = new…
2
votes
2 answers

Render CheckBox to Image for Tile

I want to display one or more CheckBoxes on a tile in my Windows Phone app. This works already for TextBlocks, but with a CheckBox it shows only the Text of the CheckBox and not the Checkmark itself. This is a sample of my code: public void…
zirkelc
  • 1,451
  • 1
  • 23
  • 49
2
votes
1 answer

Value does not fall within the range exception in WriteableBitmap

I am using WriteableBitmap in my WindowsPhone application to convert UIElement into WriteableBitmap. I am getting Value does not fall within the expected range exception randomly in the following code. But I am not able to find the root cause. Could…
David Bekham
  • 2,175
  • 3
  • 27
  • 56
2
votes
2 answers

Load BitmapImage into WriteableBitmap but no method existing

The constructor of WriteableBitmap class with Windows 8 only takes two arguments: the height and the width of this object. Meanwhile with Silverlight it accepts a BitmapImage object as argument. (Verified on MSDN : WriteableBitmap.WriteableBitmap…
StevenTB
  • 405
  • 5
  • 19
2
votes
1 answer

TextBlock Rendered On WriteableBitmap on Windows Phone isn't as crisp

I'm working with Windows Phone 8/C# Silverlight and using code similar to this to render text: TextBlock drawStringInstance = new TextBlock(); drawStringInstance.Text = str; drawStringInstance.Opacity = 1; drawStringInstance.Measure(new…
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
2
votes
1 answer

WriteableBitmapRenderer.RenderAsync() ArgumentException "Value does not fall within the expected range"

I'm developing a WP8 app using Nokia Imaging SDK. I'm trying to add filter effect to an image and render it into a WriteableBitmap. Here is my code: private async void PhotoChosen(object sender, PhotoResult photoResult) { if (photoResult…
2
votes
1 answer

Draw text on WriteableBitmap

I would like to draw some text on a WriteableBitmap. This doesn't seem possible, but I found converting WriteableBitmap to Bitmap in C# here and this. Here's the call (with conversion method included, too): // Draw dino names... …
zetar
  • 1,225
  • 2
  • 20
  • 45
2
votes
1 answer

Windows Phone - WriteableBitmap.Render does not work with InkPresenter

I'm using WriteableBitmap.Render to convert an InkPresenter control to a byte array and Image. This is my code: var bitmap = new WriteableBitmap(element, null); bitmap.Render(element, null); bitmap.Invalidate(); …
2
votes
2 answers

NV12 Format support in Bitmapimage C#

Does BitmapImage or WriteableBitmapImage supports NV12 format. or Can we create a BitmapImage or WriteableBitmapImage from a byte array which is arranged in NV12 format? Thanks
Madu
  • 4,849
  • 9
  • 44
  • 78
2
votes
1 answer

C# Out Of Memory/COMException that happens only on HP Z620 station

Our team works on a project, it's purpose is to create a renderer, which will render data on the screen. We used to work on a HP Z600 station with the following specs: RAM: 6GB 6 Cores Processor: Intel(R) Xeon(R) CPU X5650 @ 2.67GHz 2.66GHz OS:…
2
votes
1 answer

Create screenshot with app bar WP8

In my application there is a possibility to create screenshot of current view. I implement it with WriteableBitmap class. For example: var frame = Application.Current.RootVisual as PhoneApplicationFrame; WriteableBitmap bitmap = new…
2
votes
1 answer

Windows Phone 8: How can I render an invisible Map to a bitmap?

Here's what I have so far: const int imgSize = 210; Map tempMap = new Map { Width = imgSize, Height = imgSize }; // ... adding some layers, setting the view Size size = new Size(imgSize, imgSize); tempMap.Measure(size); tempMap.Arrange(new Rect(new…
Lucas Werkmeister
  • 2,584
  • 1
  • 17
  • 31