My app uses the camera to take an image and upload it to flickr. I'd like to compress the image down so that the upload doesn't take as long as it does currently. I tried both the BitmapSource and the WriteableBitmap's 'SaveJpeg' method to accomplish this but failed. Bitmap source doesn't have the same members available in Silverlight/WP as it does in the full .NET framework version and the SaveJpeg method the WriteableBitmap has kept giving me an 'This stream does not support writing to it' error.
This is what I am currently doing in my CameraCaptureTask completed event handler:
private void CameraCaptureCompleted(object sender, PhotoResult e)
{
if (e == null || e.TaskResult != TaskResult.OK)
{
return;
}
BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};
bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);
// Encode the WriteableBitmap object to a JPEG stream.
writeableBitmap.SaveJpeg(e.ChosenPhoto, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
}
This code gives me a : "Stream does not support writing" error.
Is there some other way I could compress an image or would I have to write a compression algorithm?
UPDATE FIXED!!
private void CameraCaptureCompleted(object sender, PhotoResult e)
{
if (e == null || e.TaskResult != TaskResult.OK)
{
return;
}
BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};
bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);
// Encode the WriteableBitmap object to a JPEG stream.
writeableBitmap.SaveJpeg(new MemoryStream(), writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
}
I was trying to write to the source stream. Doh!
Thanks.