2011-09-06 56 views
3

我有一个类需要一个流来旋转手机相机的图像。我遇到的问题是,当从独立存储装载图片(即用户先前保存图片后)时,它会加载到BitmapSource中。BitmapSource转换为流Windows Phone

如果可能,我想将位图源“提取”回流中?有谁知道它是否使用Silverlight for WP7?

感谢

回答

5

这给一试:

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

using (MemoryStream stream = new MemoryStream()) { 

    bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100); 
    return stream; 
} 
+2

对我来说简化版,工作! [WriteableBitmap](https://msdn.microsoft.com/pt-br/library/system.windows.media.imaging.writeablebitmap(v = vs.110).aspx)没有SaveJpeg方法。 – Butzke

2

你不必直接拉回来,成位图源,但你可以通过IsolatedStorageFileStream类到达那里。

这里是我的你的类的版本,其方法接受一个流(你的代码显然比我的更多,但这应该足以满足我们的需求)。

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

然后调用与我们从独立存储拉到文件数据类:

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; 
     } 
    } 
} 

请注意,我们使用IsolatedStorageFileStream对象从OpenFile方法直接返回。这是一个流,这是ConvertToBitmapSource所期望的。

让我知道,如果这就是你要找没有什么,或者如果我误解你的问题......

1
 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); 
     } 
相关问题