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
0 answers

Silverlight and ASP.net Bitmap converting issue

my application take snapshots using webcam and save them in a local folder. so i'm using following code to convert my Silverlight Snapshot to a byte array and send it through the Web Service. name of my web service is…
Ramesh Jaya
  • 671
  • 1
  • 10
  • 16
2
votes
1 answer

Double Buffering with vsync (in sync with refresh rate of screen) - C# windows forms

I've been trying to update the BMP of a PictureBox 60 times per second with a line pattern that changes for every update. What's happening is the image is being partially updated in between screen refreshes. So, what you see is part of one pattern…
2
votes
1 answer

Prevent image clipping during rotation Windows Phone

I am using the WriteableBitmapEx extension method to rotate a WriteableBitmap. bi in the code is a WritableBitmap. The RotateFree method rotates the bitmap in any degree and returns a new rotated WritableBitmap. My code: private void rotate() …
PutraKg
  • 2,226
  • 3
  • 31
  • 60
2
votes
0 answers

rendering usercontrol live tile windows phone

Looks like I managed to solve it by calling UpdateLayout on my usercontrol before and after each measure and arrange calls, working good so far. if anyone has another solution please let me know I have a problem similar to the one in this post and…
2
votes
3 answers

How to create a writeablebitmap image from an image file in windows8

I want to load an image file from local disk to a writeablebitmap image, so I can let users to edit it. when I create a WriteableBitmap object, the constructor need pixelwidth and pixelheight parameters, I don't know where to get these two, anyone…
James
  • 2,570
  • 7
  • 34
  • 57
2
votes
1 answer

WPF WriteableBitmap problems with transparency

First of all Ill explain what I try to do, just in the case that someone comes up with a better approach I need to blend too images using the "color" stile from photoshop (you know, the blending methods: Screen, hard light, color ... ) So, I have…
javirs
  • 1,049
  • 26
  • 52
2
votes
2 answers

WinRT: how to save WriteableBitmap to localfolder

How to save a WriteableBitmap to localfolder using C# in WinRT?
Andy Wan
  • 1,090
  • 2
  • 11
  • 23
2
votes
1 answer

How to copy an image from PhotoChooserTask to a Bitmap

I have been attempting to make a copy of a picture that the user selects from the PhotoChooserTask and use this as a background image for a hubtile in my application, but I cannot figure out the properr way to accomplish this. As of now, I can set…
Matthew
  • 3,976
  • 15
  • 66
  • 130
2
votes
1 answer

WriteableBitmapEx not working for C# metro app

I downloaded WriteableBitmapEx for winrt (win 8 metro). I added a reference to the dll with the extension methods , used them in my code , every thing compiles ok. The problem is that the extension methods have no effect. Here is part of my…
2
votes
3 answers

In WinRT, how do I load an image and then wait only as long as is needed for it to load before writing to it?

I'm using WriteableBitmapEx in a WinRT project. I load an image into a WriteableBitmap from the users Picture library. However, I cannot then immediately write to that image, if I do, it will be overwritten with the image itself (it seems like…
1
vote
3 answers

Creating WriteableBitmap form chart issue

I have a problem when trying to create writeable bitmap form Silverlight toolkit Graph. When using textBlock, everything is fine, but after trying to use Chart, generated bitmap is empty :( . var data = new List(100); for (int i = 0; i < 100;…
1
vote
2 answers

Projection is not being respected when rendering a UIElement into a WriteableBitmap

I am using Silverlight 4 and am having trouble correctly generating bitmaps from UIElements that have a projection applied to them. In my specific case I am using the WriteableBitmap class to capture a bitmap of an Image control which has a…
Pete
  • 155
  • 1
  • 5
1
vote
1 answer

Save as a image all the content of a grid

So, I'm trying to save an canvas content as an image. I'm using this for it: var img = new WriteableBitmap(myCanvas, null); Problem is that the image is not showing all the content inside of the canvas. If there's a button, an image or other…
Boga
  • 371
  • 3
  • 14
1
vote
1 answer

Why doesn't Silverlight support BMP/JPEG/PNGBitmapEncoder?

I am trying to save a WriteableBitmap to local in Silverlight using SaveFileDialog. A question similar to mine is asked Here. And the answers show several opensource encoders and workarounds. They work fine. But why silverlight doesn't support…
vicancy
  • 323
  • 3
  • 11
1
vote
1 answer

Take screenshot Webbrowser in wp7

I have a code to take screenshot in wp7. int Width = (int)LayoutRoot.RenderSize.Width; int Height = (int)LayoutRoot.RenderSize.Height; // Write the map control to a WwriteableBitmap WriteableBitmap screenshot = new WriteableBitmap(LayoutRoot, new…