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

How to add an image of a Silverlight UiElement to Word Document

I want to add an image of a certain DataGrid in silverlight to a word Document. I've created a WriteableBitmap of the DataGrid but I can't find how to copy this image to the word document. It doesn't have to be a WriteableImage. Any solution that…
2
votes
1 answer

Writeablebitmap - scrolling 1 px to the left, what's the best way ? (AKA Where's memmove ?)

I have a writeablebitmap. I want to scroll the contents 1 pixel to the left, and fill in a new pixelrow in the rightmost column. In C++ I'd memmove the entire buffer 1 pixel to the left, and overwrite the last pixel of each line - but I don't know…
Pygmy
  • 1,268
  • 17
  • 33
2
votes
1 answer

WP7: IsolatedStorage vs. WriteableBitmap

I have a scenario that I need some good solid advice on. The question is really about speed of WriteableBitmap vs. images in IsolatedStorage on the Windows Phone. I have an app that displays a UserControl (#1) which is a little graphically heavy.…
Todd Main
  • 28,951
  • 11
  • 82
  • 146
2
votes
1 answer

How to adjust the brightness of an Image efficiently

Does anyone know of a more efficient way of adjusting the brightness of an image at runtime in UWP? I found this question which works fine but runs terribly slow. However, I can't find any documentation online suggesting there is an alternative…
Adam McMahon
  • 765
  • 3
  • 24
2
votes
1 answer

C# UWP WriteableBitmap to MediaClip conversion

I'm working with UWP app. I'm generating set of WriteableBitmap's and after that, i want to add it to MediaComposition and play in video player. I found one way how to do this, but it's really slow. Here is my code: var clip = await…
2
votes
1 answer

Unwanted red border around a WriteableBitmap

I'm playing around with fractals as a private project to practice (to leave beginner level^^). I have a canvas holding an image, which is bound to a WriteableBitmap:
Roland Deschain
  • 2,211
  • 19
  • 50
2
votes
1 answer

How can i convert WriteableBitmap to jpg or some other common format?

I have one application that allows user to do various things like rotating and scaling an image and finally when the user clicks on save, the image should get saved on the server. Everything works out fine but the problem is since I am directly…
TCM
  • 16,780
  • 43
  • 156
  • 254
2
votes
1 answer

Converting a WriteableBitmap image ToArray in UWP

I am accessing an image from a blob storage on my azure service. I am returning the uri for the image and then uses HttpClient to try and download it. The uri is verified to be correct. using (HttpClient client = new HttpClient()) { try { …
JTIM
  • 2,774
  • 1
  • 34
  • 74
2
votes
1 answer

UWP - How to use WriteableBimap in a background task? Alternative?

The following code reads an image, and centers (cuts and copies) that image into a resulting image with a black frame, returns the new image. Can i do it without using a writeablebitmap class? private async Task
pijemcolu
  • 2,257
  • 22
  • 36
2
votes
1 answer

Get WriteableBitmap from url in UWP?

So my app gets a BitmapImage from a url, which works, and I then need to assign that image to a writeablebitmap. For the moment I have this: Debug.WriteLine("Poster opened for loading. Url=" + e); BitmapImage img = new…
user2950509
  • 1,018
  • 2
  • 14
  • 37
2
votes
1 answer

Inverting WriteableBitmap colors in Pdf Reader (UWP)

I'm trying to Invert color of a page in my UWP pdf reader (using Windows.Data.Pdf) . Piece of my code: using Windows.Data.Pdf; private PdfPageRenderOptions _ro; private PdfPage _page; private WriteableBitmap wb; ... private async void…
Shahriar
  • 939
  • 1
  • 14
  • 36
2
votes
1 answer

Does WriteableBitmap has new features in Silverlight 4?

and if someone could give me an example on how to use it, with a stream? (not sure how that works) I know how to create a BitmapImage from an URI now I need to convert this image to a WriteableBitmap, but I get a null exception error with something…
david
  • 741
  • 3
  • 10
  • 29
2
votes
2 answers

WriteableBitmap in .NET 4

I have got a problem: I would like to use a method Render() for WriteableBitmap object. However, as I noticed, the method is not available without using a Silverlight assembly System.Windows.dll. I need to use RenderTargetBitmap in my project…
Jamie
  • 1,092
  • 3
  • 22
  • 44
2
votes
2 answers

How does one enhance Silverlight WritableBitmap quality when replacing UIElements for animations

I'm doing an animation where I temporarily drop down the visibility of UIElements and overlay WritableBitmap versions of the original Image. The problem I have is that when I do this on element with text, it results in a noticeably blurry and…
Stephen Ellis
  • 2,561
  • 2
  • 24
  • 47
2
votes
0 answers

How to achieve hittest behavior in WriteableBitmap

I have a image (System.Windows.Controls) class. I have given WriteableBitmap to image source and drawn line in image. I need to show the tooltip on the line when mouse over the line region. How can I achieve this?
Muneesh Kumar
  • 196
  • 1
  • 1
  • 10