2011-09-06 59 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); 
     } 
相關問題