Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapSource into a stream Windows Phone

I have a class which requires a Stream to rotate an image from the phone camera. The problem I have is that when loading the picture from isolated storage (ie after the user has saved the picture previously) it is loaded into a BitmapSource.

I would like to 'extract' the bitmap source back into a stream if possible? does anyone know if it is using silverlight for WP7?

Thanks

like image 455
Tim Windsor Avatar asked Jul 22 '26 14:07

Tim Windsor


2 Answers

Give this a try:

WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);

using (MemoryStream stream = new MemoryStream()) {

    bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
    return stream;
}
like image 181
Amulya Khare Avatar answered Jul 25 '26 03:07

Amulya Khare


You don't have to pull it back into a BitMap source directly, but you can get there through the IsolatedStorageFileStream class.

Here's my version of your class whose method accepts a stream (your code obviously does more than mine, but this should be enough for our purposes).

public class MyPhotoClass
{
    public BitmapSource ConvertToBitmapSource(Stream stream)
    {
        BitmapImage img = new BitmapImage();
        img.SetSource(stream);
        return img;
    }
}

Then calling that class with data from the file we pulled from Isolated Storage:

private void LoadFromIsostore_Click(object sender, RoutedEventArgs e)
{
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fs = file.OpenFile("saved.image", FileMode.Open))
        {
            MyPhotoClass c = new MyPhotoClass();
            BitmapSource picture = c.ConvertToBitmapSource(fs);
            MyPicture.Source = picture;
        }
    }
}

Note that we're using the IsolatedStorageFileStream object returned from the OpenFile method directly. It's a stream, which is what ConvertToBitmapSource expects.

Let me know if that's not what you're looking for or if I misunderstood your question...

like image 44
Chris Koenig Avatar answered Jul 25 '26 04:07

Chris Koenig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!