5

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.

golamrabbi
  • 59
  • 1
  • 7
Cranialsurge
  • 245
  • 5
  • 13
  • PhotoResult e is not a stream so SaveJpeg will not work targeting it, where do you want to save your picture to? IsolatedStorage or a temporary stream or somewhere else... – ameer Feb 16 '12 at 22:19
  • 1
    Just so you know, when you save it to new MemoryStream() you have no reference to where you saved it now, best to create the memory stream before, pass it the memory stream then once you get it compressed either upload it directly from the memory stream or save it to isolated storage and upload it later. – ameer Feb 16 '12 at 22:45

1 Answers1

3

SaveJpeg is the way I would do this I think. You could probably do it some other ways but I think that would be the easiest and most natural. The error 'This stream does not support writing to it' is likely because whatever stream you are passing to SaveJpeg it isn't writable. I'm not exactly sure what you are trying to write to, try using just a plain old memory stream and see if that works like such

using System.IO;

// ...

MemoryStream ms = new MemoryStream();
pic.SaveJpeg(ms, pic.PixelWidth, pic.PixelHeight, 0, 0, 50);

You can adjust the quality in the final parameter. PixelWidth/Height is from WriteableBitmap so if you have some other source you might have to use another method/property to get the width/height. You might want to scale these since the pictures from the camera can be pretty big. It depends on what you're uploading these pictures for but scaling them you can make the file size smaller as well.

ameer
  • 2,598
  • 2
  • 21
  • 36