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

How to save C# .NET WriteableBitmap that is 32767 x 32767 in resolution as JPG or PNG

This is for a .NET 4.5 Console Application running on a 64-bit Windows 8 system with 32GB of memory. The application is targeted for 64-bit platforms and has the gcAllowVeryLargeObjects flag enabled, which allows the application create objects…
Seong Yup Yoo
  • 335
  • 1
  • 3
  • 11
4
votes
2 answers

WriteableBitmap.Lock() Performance Issue

I have an application where performance-sensitive drawings occur using a WriteableBitmap. An event is called with CompositionTarget.Rendering to actually update the back buffer of the WriteableBitmap. From the MSDN documentation, that means the…
red_sky
  • 834
  • 9
  • 17
4
votes
1 answer

User created pixel byte array does not appear to update correctly (WPF)

I have a system where I am able to collect 8-bit Gray images from a camera, place the data into a WriteableBitmap and display the images on a WPF Image object. This work happens in a camera thread. I used this article to help me: How to create a…
CDitchman
  • 65
  • 5
4
votes
3 answers

WriteableBitmapEx.DrawRectangle Thickness?

How do you change the thickness/weight for the rectangle outline drawn with the WriteableBitmapEx.DrawRectangle extension method? The code I use to draw the rectangle is: WriteableBitmap wbmp = new WriteableBitmap(bmp); wbmp.DrawRectangle(0, 0, 480,…
msbg
  • 4,852
  • 11
  • 44
  • 73
4
votes
2 answers

Why do my images sometimes reload (and WriteableBitmaps disappear) on resume?

When I resume my app, sometimes it loses its images. I can reproduce this even in trivial apps. This manifests in two different ways, depending on where the image came from: If I did something like (i.e., loading…
Joe White
  • 94,807
  • 60
  • 220
  • 330
4
votes
1 answer

Setting line stroke thickness in WriteableBitmap DrawLine method

i am using WriteableBitmap to draw lines as WriteableBitmap writeableBmp=new WriteableBitmap(500,500); writeableBmp.DrawLineDDA(10, 20, 50, 70,myIntColor); But here i am not getting any method to set the line stroke thickness. I want to set the…
benjamin54
  • 1,280
  • 4
  • 28
  • 48
4
votes
2 answers

How do I add watermark text to a bitmap in WinRT?

I would like to do something like this http://weblogs.asp.net/broux/archive/2011/02/08/silverlight-how-to-watermark-a-writeablebitmapimage-with-a-text.aspx. I am having a tough time getting this to work in WinRT. I am using the WriteableBitmap…
4
votes
1 answer

Converting from Pbgra32 to Bgr32 PixelFormats using FormatConvertedBitmap

I am having trouble understanding how to use FormatConvertedBitmap to convert a WriteableBitmap I have from Pbgra32 to Bgr32. The application I'm building was initially using Bgr32, and I introduced WriteableBitmapEx (which uses Pbgra32) to flip an…
cryptic_star
  • 1,863
  • 3
  • 26
  • 47
4
votes
1 answer

Rendering a text block with WriteableBitmap and RotateTransform on WP7

it seems that there is a really annoying bug in WriteableBitmap for Silverlight for Windows Phone. I have the following code and xaml: public partial class MainPage : PhoneApplicationPage { CompositeTransform rotate = new CompositeTransform(); …
Viktor
  • 39
  • 2
3
votes
1 answer

WriteableBitmap access violation problem

The following code always causes an AccessViolationException in the Fill method when the last pixel (x = 255, y = 255) is accessed. However, if I use a size such as 200x200 it works. Same problem with 512 x 512 or 1024 x 1024 or other power-of-two…
mbuchetics
  • 1,370
  • 1
  • 17
  • 34
3
votes
1 answer

C# passing property of viewmodel into dispatcher

I have a method which updates the contents of a WriteableBitmap (FrameDataNew) which is a property in my viewmodel (VM): public WriteableBitmap FrameDataNew { get { return frameDataNew; } set { …
MrVanx
  • 41
  • 1
  • 6
3
votes
1 answer

WriteableBitmap failing badly, pixel array very inaccurate

I have tried, literally for hours, and I have not been able to budge this problem. I have a UserControl, that is 800x369, and it contains, simply, a path that forms a worldmap. I put this on a landscape page, then I render it into a…
3
votes
0 answers

WritableBitmap memory usage

I've been writing an assembly to handle some basic image manipulation and have hit a stumbling block that I am a bit stuck on resolving. The crux of the issue is when I create a new System.Windows.Media.Imaging.WriteableBitmap, the resultant private…
3
votes
2 answers

WriteableBitmap.WritePixels not refreshing on some PCs

Very odd issue we have hit with some Windows PCs. We have a video application and one part uses WritePixels to update a WriteableBitmap and display the frames to screen. Code is: _currentframe.WritePixels(new Int32Rect(0, 0, data.Width,…
Sugrue
  • 3,629
  • 5
  • 35
  • 53
3
votes
2 answers

Resizing WritableBitmap

I have created a WriteableBitmap in Gray16 format. I want to resize this WriteableBitmap to my known dimention preserving the pixel format(Gray16). Is any one worked on the Resizing the WriteableBitmap. Please help me. I also searched the internet…
Harsha
  • 1,861
  • 7
  • 28
  • 56
1 2
3
18 19