将BitmapSource转换为Windows Phone流

我有一个类要求Stream从手机摄像头旋转图像。 我遇到的问题是,当从隔离存储器加载图片时(即在用户之前保存图片之后),它被加载到BitmapSource中。

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

谢谢

尝试一下:

WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img); using (MemoryStream stream = new MemoryStream()) { bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100); return stream; } 

您不必直接将其拉回到BitMap源中,但您可以通过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; } } } 

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

如果这不是你想要的,或者我误解了你的问题,请告诉我……

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