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

PixelBuffer.AsStream() method obsolete?

I was checking the MS library. and it looks like PixelBuffer.AsStream() is obsolete for the Universal App development. I'm using Win10(build 10240) with VS2015 Community version. Do I miss anything ? Additionally, what's the best way to encode or…
Carl S.
  • 69
  • 1
  • 1
  • 8
0
votes
1 answer

Create WriteableBitmap in windows phone app 8.1?

iam creating qr code scan in windows phone app. using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) { wrb = await Windows.UI.Xaml.Media.Imaging.BitmapFactory.New(1, 1).FromStream(fileStream); …
Anil Kumar
  • 303
  • 1
  • 6
  • 23
0
votes
1 answer

HTML Silverlight bridge and WriteableBitmap

I'm trying to implement the Silverlight HTML Bridge and the function is not executing properly when triggered from the HTML side. I am using Silverlight 5. Problem I have reduced the example to rendering a rectangle on a WriteableBitmap. My actual…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0
votes
0 answers

Stack polygons on top of each other, join by diagonal lines

I want to do a perfectly simple thing. I want to draw two quadrilateral polygons, which have the top and bottom lines as parallel diagonals. I also want to draw these polygons stacked on top of each other. However, after I draw the two polygons,…
user3079266
0
votes
1 answer

Bitmap to WriteableBitmap C#

Hi guys I'm trying to get a pixel value from an image taken using a windows phone camera. But it seems as though I am creating an empty writeable bitmap instead of converting the photo loaded into a writeable bitmap. I'm a beginner with c#. Any…
0
votes
0 answers

Getting pixel rgb value using writeablebitmapex c#

I'm creating an app that allows the user to upload/take an image and then see the rgb code from a pixel in that image. ATM I'm trying to get a pixel rgb code to show in the ui under the image. But I'm struggling to get any data at all. At the…
0
votes
2 answers

How to get RGB code from bitmap

To start things off I'm fairly new to C#, XAML and windows phone. So far I have managed to get a photo into the app via gallery/camera and turn it into grayscale. But my aim is to get the rgb values from a pixel and display it under the image as…
0
votes
0 answers

How do I construct a C# .NET WIC WriteableBitmap at is maximum resolution?

According to this -> https://social.msdn.microsoft.com/Forums/vstudio/en-US/58710bdd-4832-4531-a50a-b83a7e733cdd/maximum-size-of-a-writeablebitmap-systemwindowsmediaimagingwritablebitmap?forum=wpf The maximum height and width of an image is 2^16…
Seong Yup Yoo
  • 335
  • 1
  • 3
  • 11
0
votes
2 answers

WriteableBitmap.Render changing color in the image

So I am trying to save a png of a UserControl to use as a tile. The code that i am using is SquareTile sq = new SquareTile(); sq.Measure(new Size(336,336)); sq.Arrange(new Rect(0,0,336,336)); RenderExtensions.SaveToFile(sq); And the…
Rishabh876
  • 3,010
  • 2
  • 20
  • 37
0
votes
1 answer

Weird WriteableBitmap FromStream issue

I am desperately trying to load a WriteableBitmap with the FromStream extension method. The code below throws a System.InvalidOperationException at the FromStream() call with the following information: BitmapImage has not been initialized. Call…
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0
votes
1 answer

Photo to Base64 in Windows Phone 8.1

I am writing a Windows Phone 8.1 App (WINPRT). User picks photo from gallery and this image has to be uploaded on server. So, i need to convert it into Base64 string. So, flow I am following for Photo to Base64 string is: args.Files[0] > StorageFile…
0
votes
1 answer

when to use WriteableBitmap and BitmapImage in silverlight

I am trying to display image using BitmapImage for some time and it worked.I have changed the image and it stopped working. For Bitmapimage I was using this code: `ms.Seek(0, SeekOrigin.Begin); // ms is memory stream …
user2526236
  • 1,538
  • 2
  • 15
  • 29
0
votes
1 answer

Create a WriteableBitmap from a byte array wpf

from examples I have been able to create BitmapImage the byte array public byte[] BufferFromImage(BitmapImage myImageFile) { WriteableBitmap btmMap = new WriteableBitmap(BitmapFactory.ConvertToPbgra32Format(myImageFile)); return…
mgphall
  • 67
  • 3
  • 9
0
votes
1 answer

Loading System.Windows.Controls.Image to WritableBitmap object

I've created Image object in xaml code: In code-behind I've written: WriteableBitmap bitmap = new WriteableBitmap((int)Square.Width, (int)Square.Height, 96, 96, PixelFormats.Rgb24,…
wsc
  • 50
  • 1
  • 8
0
votes
1 answer

How to divide Image into 5*5 pieces Windows phone?

I am developing an application where i have to divide an image into separate images, i have tried with WriteableBitmap , here is my code. Is there any better way to achieve the same? XAML
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396