Here is the problem. I want to open a file from local drives, then make it into a WritableBitmap so i can edit it. But the problem is, i cannot create a WritableBitmap from Uri or something like that. Also i know how to open a file into BitmapImage but i cannot figure out how to open a file as WritableBitmap. Is there way to open a file directly into a WritableBitmap,if there is not, is there a way to convert a BitmapImage to a WritableBitmap? Thanks guys.
Asked
Active
Viewed 7,053 times
4
-
it seems that we can pass a bitmap directly into writablebitmaps constructor. My VS gave me errors while i first tried it but it seems working now. It wasn't a good question at all, sorry guys. – gkaykck Nov 23 '10 at 09:35
-
It will work, but it will be resized if above some mega pixels. – Léon Pelletier Apr 17 '13 at 15:45
3 Answers
6
You can load your image file into a BitmapImage and use that as a source for your WriteableBitmap:
BitmapImage bitmap = new BitmapImage(new Uri("YourImage.jpg", UriKind.Relative));
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);

Frédéric Hamidi
- 258,201
- 41
- 486
- 479
-
1Actually, in Siverlight, it gives me exception. So it's not a solution – Peter Lee Dec 27 '12 at 05:57
-
-
1`BitmapImage bi = new BitmapImage(new Uri("header.png", UriKind.Relative)); WriteableBitmap wb = new WriteableBitmap(bi);` The 2nd line `System.NullReferenceException was unhandled by user code: Object reference not set to an instance of an object.` – Peter Lee Dec 27 '12 at 16:30
-
@Peter, did you check that `bi` is not `null` after the call to the `BitmapImage` constructor? If it is, it would be quite logical for the `WriteableBitmap` constructor to fail. – Frédéric Hamidi Dec 27 '12 at 16:34
-
Yes, I did check. See my answer. Would you mind test my code on your machine? – Peter Lee Dec 27 '12 at 18:58
-
Loading a WriteableBitmap from a BitmapImage is actually a workaround as the final image is scaled to physical device limitations. Thus, a 5MP can be rescaled to a 1MP image. Not useful, considering WriteableBitmap is used for image processing. – Léon Pelletier Apr 17 '13 at 15:42
-
@Peter Lee. What do you get when using CreateOptions set to None? – Léon Pelletier Apr 17 '13 at 15:43
-
@LéonPelletier i set creation option to None, Error exist (Silverlight 5). In created bitmap image source PixelHeight and PixelWidth is zero. – shalin Jul 16 '15 at 04:17
1
I'm no expert and don't have immediate access to intellisense and whatnot, but here goes...
var fileBytes = File.ReadAllBytes(fileName);
var stream = new MemoryStream(fileBytes);
var bitmap = new BitmapImage(stream);
var writeableBitmap = new WritableBitmap(bitmap);
Even if not a perfect example this should be enough to point you in the right direction. Hope so.

Grant Thomas
- 44,454
- 10
- 85
- 129
-
-
`File.ReadAllBytes(fileName);` not allowed in Silverlight, unless in OpenFileDialog – Peter Lee Dec 27 '12 at 07:46
0
In Silverlight 5 you can use below method to open file from local disk and convert it to BitmapImage and make it in to WriteableBitmap;
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.ShowDialog();
byte[] byteArray = new byte[] { };
if (dlg.File == null) return;
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
// bi.UriSource = new Uri(@"C:\Users\saw\Desktop\image.jpg", UriKind.RelativeOrAbsolute);
bi.SetSource(dlg.File.OpenRead());
WriteableBitmap eb=new WriteableBitmap(bi);
setting new Uri gave me error (Object reference not set to an instance of an object) while trying to create WriteableBitmap

shalin
- 443
- 4
- 22