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

How to WritePixels (array of ushort[]) to WritableBitmap BGR32

I'm working with WriteableBitmap (PixelFormats.Bgr32). This is the format I need to save for the rest of the program to work. Writable = new WriteableBitmap(inImage.XSize, inImage.YSize, dpi, dpi, PixelFormats.Bgr32, null); The image from the…
1
vote
0 answers

Performance difference between running program by Visual Studio and by local ( using WriteableBitmap to draw in WPF)

I have a WPF application for drawing (Like Microsoft Paint) and I use WriteableBitmap and subscribe mouse event to do that. It works perfectly and the ink tracks my mouse immediately when I run the program by Visual Studio no matter in debug mode or…
BearCho35
  • 11
  • 2
1
vote
1 answer

How do I get an image from a file in a silverlight C# library into a writeable bitmap without using ImageLoaded event?

I need to have an image in my silverlight library and load it into a bitmap. I want to just refer to it like a resource, but not sure how to go about it. I don't have any xaml at all in this library, but what I read seems to indicate I need to do…
Brad Boyce
  • 1,248
  • 1
  • 17
  • 34
1
vote
1 answer

Threading WriteableBitmap to Image control

I am using OpenGL to render a static picture in WPF, on a .NET Core 3.1 platform. The context is initially displayed in a hidden GLFW window. Then I am using glReadPixels to read the context and display it on an Image control on the UI, using the…
Aris Theo
  • 55
  • 5
1
vote
1 answer

Artifacts on Avalonia WriteableBitmap BitmapContext

I am trying to write WriteableBitmapEx BitmapContext for Avalonia WrieableBitmap class. Code successfuly sets pixels of color I want, and gets them, but BitmapContext messes some pixels with color I didn't set. For example I provided it should set 4…
flabbet
  • 327
  • 3
  • 14
1
vote
0 answers

UWP Compatible package for System.Windows.Media.Imaging.WriteableBitmap and System.Windows.Media.PixelFormats

I have a WPF pdf viewer code. I'm trying to convert it into UWP. To render the pages of a pdf, WPF uses System.Windows.Media.Imaging.WriteableBitmap. UWP compatible Windows.UI.Xaml.Media.Imaging.WriteableBitmap doesn't contain all the…
1
vote
0 answers

how can i make setpixel like method in wpf?

I need a very fast method to update bitmap, something like setpixel in wpf. I don't know much about imaging in wpf ? I made my own method but it is very-very slow. I also tried to use http://writeablebitmapex.codeplex.com/, but it is for Silverlight…
Code0987
  • 2,598
  • 3
  • 33
  • 51
1
vote
1 answer

Change bitmap palette for an image

I have a TIFF image for which I wish to change the BitmapPalette for a specific frame of the image. I have the following code: var width = image.Frames[0].PixelWidth; var height = image.Frames[0].PixelHeight; var dpiX = image.Frames[0].DpiX; var…
CiucaS
  • 2,010
  • 5
  • 36
  • 63
1
vote
1 answer

How to draw a rectangle using WriteableBitmap?

I created a WPF project which just includes an Image control. My goal is to click the image and draw a 10 pixel side square, of white color, at the specific position…
Themelis
  • 4,048
  • 2
  • 21
  • 45
1
vote
1 answer

How to avoid `WriteableBitmap buffer size is not sufficient` exception for Bgr24 format?

I want to create a WriteableBitmap and then set it as an Image.Source. I am interested only in RGB, with full opacity, so I chose the Bgr24 format. Now when I am trying to write some pixels to the bitmap, using WritePixels() I get the Buffer size is…
Themelis
  • 4,048
  • 2
  • 21
  • 45
1
vote
0 answers

Using WriteableBitmap to modify pixels of a JPG image

I'm fairly new to C# and WPF programming. I am currently trying to figure out how to modify a JPG image using WriteableBitmap. I think I get the general gist of whats going on, but I'm observing a strange behavior that I just don't understand. …
Iggy
  • 11
  • 4
1
vote
2 answers

Uploading a JPEG created from a WriteableBitmap without saving it to the users computer first

I have an application that allows users to upload videos to the server and part of the process is to create a thumbnail image on the client that will be used for the video when loaded on the server. I have a writeablebitmap that I want to convert to…
MIantosca
  • 833
  • 1
  • 10
  • 33
1
vote
1 answer

How do you convert a C# WritableBitmap to a EmguCV/OpenCV Mat?

I have a WritableBitmap, and I would like to convert it to a EmguCV/OpenCV Mat. How would I go about doing that? I've tried several chain solutions (WritableBitmap -> Bitmap -> Map) from code online, but I haven't found anything that works. Thanks!
Nisala
  • 1,313
  • 1
  • 16
  • 30
1
vote
1 answer

UWP Encode WriteableBitmap to JPEG byte array

I'm trying to encode WriteableBitmap to JPEG byte array. But actually I found examples with the SaveJpeg method which does not exist now. So I tried the ToStreamAsJpeg method but this method does not work. With the step by step I don't get out of…
Samuel LIOULT
  • 594
  • 5
  • 21
1
vote
1 answer

How do i get the color of a pixel from a WriteableBitmap?

I tried everything, but the WriteableBitmap class just doesn't have the .CopyPixels() method like it is said in the droumentation:https://msdn.microsoft.com/de-de/library/system.windows.media.imaging.writeablebitmap_methods(v=vs.110).aspx Example of…
Fabian
  • 23
  • 1
  • 4