Monday, December 31, 2012

Opening an image file into WritableBitmap

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.

Answers & Comments...

Answer: 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.

by : Grant Thomashttp://stackoverflow.com/users/263681

Answer: 2

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); 
by : Frédéric Hamidihttp://stackoverflow.com/users/464709

Answer: 3

About Frédéric Hamidi's Answer:

private void btnTest_Click(object sender, RoutedEventArgs e) {     BitmapImage bi = new BitmapImage(new Uri("header.png", UriKind.Relative));      if (bi == null)     {         MessageBox.Show("BitmapImage is null");     }     else     {         WriteableBitmap wb = new WriteableBitmap(bi); // Exception throw on this line         MessageBox.Show(wb.PixelHeight.ToString());     } } 

Exception details:

System.NullReferenceException was unhandled by user code   Message=Object reference not set to an instance of an object.   StackTrace:        at MS.Internal.XcpImports.CheckHResult(UInt32 hr)        at MS.Internal.XcpImports.WriteableBitmap_CreateFromSource(WriteableBitmap wb, IntPtr ptrMemory, BitmapSource source, Boolean& bHasProtectedContent)        at System.Windows.Media.Imaging.WriteableBitmap..ctor(BitmapSource source)        at SilverlightApplication2.MainPage.btnTest_Click(Object sender, RoutedEventArgs e)        at System.Windows.Controls.Primitives.ButtonBase.OnClick()        at System.Windows.Controls.Button.OnClick()        at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)        at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)        at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)   InnerException:  

Project File Structure:

- Project     - MainPage.xmal/cs     - header.png 

I tried this on two different machines (Win7 + Silverlight 5 + Visual Studio 2010 Web Developer Express Edition)

by : Peter Leehttp://stackoverflow.com/users/301336




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog