Reputation: 329
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
Upvotes: 3
Views: 6149
Reputation: 98
var background = Brushes.Transparent;
var bmp = Viewport3DHelper.RenderBitmap(viewport, 500, 500, background);
BitmapEncoder encoder;
string ext = Path.GetExtension(FileName);
switch (ext.ToLower())
{
case ".png":
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
encoder = png;
break;
default:
throw new InvalidOperationException("Not supported file format.");
}
//using (Stream stm = File.Create(FileName))
//{
// encoder.Save(stm);
//}
using (MemoryStream stream = new MemoryStream())
{
encoder.Save(stream);
this.pictureBox1.Image = System.Drawing.Image.FromStream(stream);
}
Upvotes: 1
Reputation: 7698
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;
}
Upvotes: 5
Reputation: 2748
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...
Upvotes: 2