I found this great library for Silverlight that basically a port of libjpeg, so I was all fired up to check it out. After downloading the library from the SVN repo (http://fjcore.googlecode.com/svn/trunk/), I was able to successfully build the library and add a reference to the .DLL to my Windows Phone 7 project. Upon successful build of my WP7 project, when using the emulator, the application crashes with an error I’ve never seen before:
FieldAccessException was unhandled. – System.Reflection.Emit.OpCodes.Ldarg_1
The silverlight example from the original code worked fine. The example opens up an “Open File” dialog and resizes the image based on what’s selected. Unfortunately, I can’t use the same code in my WP7 project. The WP7 classes don’t support the “Open File” dialog. The library just uses a generic Stream object, so I’m passing in the e.Result (a PhotoStream object) from a PhotoChooserTask Completed event.
Any thoughts as to why I’m getting this error and how to fix it?
Resizing the image in my WP7 application is imperative. After all, who want’s to wait to upload a 5MP image over a 3G network? I know I sure don’t.
Thanks!
UPDATE – Solution found for resizing an image on WP7 on app thread (not UI thread)
protected Stream ResizeImage(WriteableBitmap tempBitmap, int maxEdge)
{
WriteableBitmap wb = tempBitmap;
// Resize to Max Values...
Size widthHeight = GetMaxWidthHeight(wb.PixelWidth, wb.PixelHeight, maxEdge);
Stream resizedStream = new MemoryStream();
wb.SaveJpeg(resizedStream, (int)widthHeight.Width, (int)widthHeight.Height, 0, 90);
return resizedStream;
}
because you cannot use reflection (DLR) in WP7 app.