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.
Opening an image file into WritableBitmap
Asked Answered
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. –
Katha
It will work, but it will be resized if above some mega pixels. –
Monocycle
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);
Actually, in Siverlight, it gives me exception. So it's not a solution –
Muncy
@Peter, which exception do you receive? –
Cavill
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.
–
Muncy @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. –
Cavill Yes, I did check. See my answer. Would you mind test my code on your machine? –
Muncy
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. –
Monocycle
@Peter Lee. What do you get when using CreateOptions set to None? –
Monocycle
@LéonPelletier i set creation option to None, Error exist (Silverlight 5). In created bitmap image source PixelHeight and PixelWidth is zero. –
Stenger
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.
you cannot pass a bitmap directly into a writablebitmap –
Katha
File.ReadAllBytes(fileName);
not allowed in Silverlight, unless in OpenFileDialog –
Muncy 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
© 2022 - 2024 — McMap. All rights reserved.