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

writeablebitmap in windows phone 8.1

I create universal project type in vs 2013 for 8.1. In windows phone project i added reference to WriteableBitmapEx via nuget. In references i see WriteableBitmapEx.WinRT. I write code which creates WriteableBitmap from content (use image in project…
1
vote
0 answers

How to get a WriteableBitmap image from an un-rendered control (UIElemet) in Silverlight?

I've been able to create a WriteableBitmap from controls that are rendered in my Silverlight application UI and export them to an XImage via SilverPDF and include them in a PDF document. This conversion and export process works fine. Now, I'm…
lg1382
  • 71
  • 11
1
vote
0 answers

Apply grayscale effect on image

I am trying to apply a filter on an image. XAML : This is the method I use for getting the image from a…
1
vote
1 answer

Image with high resolution not working properly when sending from Windows phone 8 application

I am working on a Windows phone 8 chat application. I need to be able to send images. I am able to send images with low resolution but when I try to send high resolution images (10240 x 6400) the resulting image is not what I expected. and . As you…
Raj123
  • 971
  • 2
  • 12
  • 24
1
vote
1 answer

In WebBrowser control get Silverlight Canvas

I have a webbrowser control that loads an html page which contains a silverlight object. I want to use the webbrowser control to get the silverlight canvas so that I can pass it to a WriteableBitmap() object. The silverlight is being loaded into a…
1
vote
0 answers

C# WPF: Combination of Viewport3D and WriteableBitmap causes flickering in Viewport3D control

I have two WPF controls: (1) a viewport3D control through the use of HelixViewport3D (from the Helix 3D Toolkit: http://helixtoolkit.codeplex.com/) (2) an Image control that is written to with WriteableBitmap.WritePixels I have a Kinect sensor…
1
vote
0 answers

Improve WriteableBitmap performance

I'm working on a project were I need to draw N ellipses every X milliseconds, where 0 <= N <= 10^5 and 5 <= X <= 500. I started using WPF, and its relative controls like Canvas for the viewport and Ellipse controls for the ellipses. I store the…
Nick
  • 10,309
  • 21
  • 97
  • 201
1
vote
1 answer

How to resize pixel of WriteableBitmap (windows store c#)

I searched in many forums and I didn't get what I want. My question is clear and simple : How to resize pixel of WriteableBitmap , for example from 300*300 to 500*500
Anis
  • 43
  • 6
1
vote
1 answer

Alpha channel not working as expected with WriteableBitmap

My current code is blit'ing a small Pbgra32 bitmap onto a larger Pbgra32 bitmap. Works fine. What I would like to do now is make that smaller one partly transparent. To do this, before the blit, I am passing the smaller one to a method that should…
davecove
  • 1,001
  • 4
  • 16
  • 36
1
vote
1 answer

UIElement to PNG - Text slightly blurred

I am using ImageTool's PNG encoder to create an image. I have a Grid that contains multiple TextBlocks, each TextBlock contains dynamic text. When I create a WriteableBitmap from the grid containing the TextBlocks, I then use ImageTool's encoder to…
Chris
  • 2,148
  • 8
  • 31
  • 52
1
vote
4 answers

How to Reduce Size of Image in windows phone

I am trying to port my app in windows phone . i have to upload an image on server So it is in small Size For uploading i have done this thing in Widows Successfully but problem is when i failed in it .. here is my code for windows App public void…
Mashhood Adeeb
  • 37
  • 2
  • 11
1
vote
1 answer

Overlay bitmaps (writeablebitmap etc.) in windows store app

I have 2 bitmaps (foreground and background) which I need to overlay on top of each other to form a new bitmap and use it as the ImageBrush for a button. The code would look something like this (Windows 8.1 store app): WriteableBitmap…
alexbtr
  • 3,292
  • 2
  • 13
  • 25
1
vote
2 answers

Pixelvalues turn up negative

I want to save a writableimage in Silverlight to disk. I found this tutorial : http://kodierer.blogspot.com/2009/11/convert-encode-and-decode-silverlight.html, and have based my code on the EncodeJpeg method. But at the: pixelsForJpeg[0][x, y] =…
eflles
  • 6,606
  • 11
  • 41
  • 55
1
vote
1 answer

Draw a fluid set of circles in real time using WriteableBitmapEx

I have a WriteableBitmap object which I load a .jpg image into and show it in an image control. Now I would like to be able to let the user draw on that image with a little half-transparent brush. Right now I am handling the MouseDown and MouseMove…
1
vote
1 answer

WriteableBitmapEx - merge two images in one

I want to merge two images into one. I read that I should use WriteableBitmapEx library and use blit method. Both of my images are Image type. So, should I first convert Image type to WriteableBitmap ? Like this: var im1 =…