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

Displaying WriteableBitmap in Image in C# Visual Studio 2017

I'm using Visual Studio 2017. I've got a WriteableBitmap that I want to display to the user. To do that, I've got this component in my MainWindow.xaml: Now I try to assign the image…
Christallkeks
  • 525
  • 5
  • 18
0
votes
0 answers

Writing OpenCV CV_8UC3 image matrix into writeable bitmap

I have two monochromatic images as byte[] taken from cameras. I want to combine these images and write the combined image into a writeable bitmap. Merging images: OpenCV Using openCV, I create Mat objects (of type CV_8UC1) from the byte arrays…
kowsky
  • 12,647
  • 2
  • 28
  • 41
0
votes
1 answer

Writing text on a WriteableBitmap

I have searched around and found that most of the answers convert a WriteableBitmap to a System.Drawing.Bitmap. I am currently thinking of using WriteableBitmapEx's Blitz() function to overlay a bitmap with text onto the existing bitmap, but I don't…
John Tan
  • 1,331
  • 1
  • 19
  • 35
0
votes
1 answer

In WPF what is the proper method for drawing a line and PNGs on a bitmap that is attached to a canvas?

I don't do that much with WPF anymore and I always seem at a loss for the most basic things. I've tried the Googles but they're not helping. I've got a canvas (maybe I shouldn't use a canvas?) and I want to attach an image. The only way that I could…
zetar
  • 1,225
  • 2
  • 20
  • 45
0
votes
2 answers

Make a WriteableBitmap Transparent in Windows Phone 8.1 C# WinRT Programmatically

Is it possible to add transparency to a WriteableBitmap in Windows Phone 8.1 using C# / WinRT programmatically? I've referenced the WriteableBitmapEx library in my project for image resizing and blitting, which is quite useful, and it could also…
theMaxx
  • 3,756
  • 2
  • 27
  • 33
0
votes
2 answers

Silverlight 4 WriteableBitmap to Bitmap

This is my first Silverlight app and my first go at C#. I have a C# class library that I access from Silverlight using COM. The C# library has a method that takes a Bitmap as an argument, however from what I can see Silverlight only has a…
Snowwire
  • 530
  • 1
  • 6
  • 14
0
votes
1 answer

How to write pixels in WriteableBitmap by bytes from BitmapImage?

The goal is to store bytes of many same sized images and draw them in WriteableBitmap to create high performance video. I tried next code: public MainWindow() { InitializeComponent(); Method(); } private void…
Ziya Jafarov
  • 41
  • 1
  • 6
0
votes
1 answer

MVVM Writeablebitmap Image processing

The idea is to process a loaded image. I first wanted to process it that way, that there is a white/black line going through the image. Actual the problem is, that a new Thread (for Thread.Sleep) freezes the whole UI, while processing. And when I…
0
votes
1 answer

Handling image load exception gracefully

I'm loading user images using Silverlight 3. Everything works fine and I can set the file stream to a BitmapImage and it gets rendered OK. The problem is that if I try to load something that's not an image (like a .exe that's been renamed to .png)…
R4cOOn
  • 2,340
  • 2
  • 30
  • 41
0
votes
1 answer

C++ bitmap editing

I am trying to open a bitmap file, edit it, and then save the edited version as a new file. This is eventually to mess with using steganography. I am trying to save the bitmap information now but the saved file will not open. No errors in…
0
votes
2 answers

Windows universal app draw on WriteableBitmap

I'm trying to draw lines on a graphic which I have stored as a byte array and then save as a jpg in my windows 10 universal app. I have a byte[] which I have converted to a bitmapimage to get the pixel size using BitmapImage bImage; using…
user444707
0
votes
1 answer

How to store a lot of WriteableBitmap?

I'm working with a set of 400+ images, that I need to modify in different ways. At the end, I need to have almost 1500 images. Let's say they all are 512*512px I want to first apply this modifications in order to get my 1500 images, and keep all…
Charrette
  • 690
  • 1
  • 11
  • 29
0
votes
0 answers

WriteableBitmap generate on thread show on ui

From what i have read and found about the writablebitmap am i not able to change it in a different thread then the one that created it. But i should be able to created it on a diffrent thread then the UI thread, then freze it and parse it to the UI…
Androme
  • 2,399
  • 4
  • 43
  • 82
0
votes
1 answer

WriteableBitmap.render is not working in windows 8.1 c#

is there a way to add WriteableBitmapExwinphone.dll to my project ?
0
votes
2 answers

WriteableBitmap to PNG

I'm writing a simple WindowsStore app and I can not find out how can I save a WriteableBitmap to some memory stream in PNG image format in order to get the bytes of this PNG image. What I can find so far is how to do that on WP7.1 but it doesn't…
zavolokas
  • 697
  • 1
  • 5
  • 20