4

I am trying to let a user capture an image and add it onto the screen using an Image. However, I also need to resize this image down to about half size due to memory restrictions (12x 5MP images is never good on a phone...)

I am launching the camera task fine and it calls the Completed event. However, when I try and use DecodeJpeg I get a "The parameter is incorrect." exception.

Here is my code for resizing, where mx and my are int for dimensions. I have verified that there is something in the e.ChosenPhoto with a length of about ~5500:

WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(e.ChosenPhoto, mx, my);
Image img = new Image();
img.Source = bitmap;

The first line is where the app crashes. Any ideas?

EDIT: This also occurs with the result from the PhotoChooserTask....

golamrabbi
  • 59
  • 1
  • 7
Andrew M
  • 4,038
  • 10
  • 39
  • 67

1 Answers1

4

Try using the System.Windows.Media.Imaging - Extensions.LoadJpeg method instead of PictureDecoder.DecodeJpeg. Also make sure that the stream is positioned at the beginning of the stream. If you have already used the stream you will need to reset it using:

MyImageStream.Seek(0, System.IO.SeekOrigin.Begin)

I had a lot of problems trying to get access to the original image, especially since BitmapImage automatically resizes images over 2000x2000. If you want an image larger than 2000x2000 you have to have access to the original stream and load it into a WriteableBitmap object

If you want to see some more complex image handling code including detecting resolution from image stream using ExifLib and rotating stream using the WriteableBitmap Extensions check out the BarcodeCaptureResult class for the Silverlight ZXing Library.

UPDATE: Since all you want is to resize an image given the e.ChosenPhoto result I pulled the code from The Silverlight ZXing library. This should work:

WriteableBitmap wbBarcodeImage = new WriteableBitmap(mx, my);
Extensions.LoadJpeg(wbBarcodeImage, e.ChosenPhoto);//Load JPEG from stream into our re-sized writeable bitmap

Note that you will need to use the correct height/width ratio, otherwise you will have a black bar at the bottom or side of the image. You can use ExifLib to detect the original image size and use that to scale (see GetWriteableBitmap method in BarcodeCaptureResult linked above)

Community
  • 1
  • 1
Greg Bray
  • 13,166
  • 10
  • 76
  • 99
  • From what I saw in the docs, LoadJpeg doesn't allow me to resize an image (which will need to be under 2k * 2k anyways), but I'll try resetting the stream. I never use it before, though. – Andrew M Apr 26 '11 at 22:32
  • If all you want is to load the picture into an image that is less than 2000x2000 then just use a BitmapImage as outlined here: http://www.ginktage.com/2011/04/using-the-cameracapturetask-in-windows-phone-7/ You can then use a WriteableBitmap to resize it or use a ScaleTransform http://blogs.msdn.com/b/mikeormond/archive/2010/12/09/resizing-images-in-windows-phone-7.aspx – Greg Bray Apr 26 '11 at 22:35
  • I need to be able to have some control over size, so just using WriteableBitmap won't work. Looking at ScaleTransform now... I need to be able to resize the image just so I don't have a bunch of 2k*2k images on screen at once, so I need to be able to use the image again in a Image object (ie. no saving to disk) – Andrew M Apr 26 '11 at 22:41
  • I think Extensions.LoadJpeg will keep the original size of the WriteableBitmap, so you can create one at whatever size you want then load the image into it and it will automatically resize it. It will not stretch or skew, so you need to make sure to keep the original aspect ratio the same. Use ExifLib to get information about the original size. – Greg Bray Apr 26 '11 at 23:38