gkaykck
gkaykck

Reputation: 2367

Opening an image file into WritableBitmap

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.

Upvotes: 4

Views: 7470

Answers (3)

shalin
shalin

Reputation: 452

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

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

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);

Upvotes: 6

Grant Thomas
Grant Thomas

Reputation: 45083

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.

Upvotes: 1

Related Questions