将BitmapSource转换为MemoryStream [英] Convert BitmapSource to MemoryStream
问题描述
如何将BitmapSource转换为MemoryStream.虽然我尝试了一些代码:
How Do I convert BitmapSource to MemoryStream. Though I tried some code:
private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
Stream bmp;
using (bmp = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(writeBmp));
enc.Save(bmp);
}
return bmp;
}
它没有给出任何错误,但是在放置调试点之后,它显示了一些异常,如下所示.
It doesn't give any error but after putting debugging point it is showing some exceptions which are listed below.
容量:'printStream.Capacity'引发了类型异常 'System.ObjectDisposedException'长度:'printStream.Length'抛出 类型'System.ObjectDisposedException'的异常位置: 'printStream.Position'引发了类型异常 'System.ObjectDisposedException'
Capacity: 'printStream.Capacity' threw an exception of type 'System.ObjectDisposedException' Length: 'printStream.Length' threw an exception of type 'System.ObjectDisposedException' Position: 'printStream.Position' threw an exception of type 'System.ObjectDisposedException'
推荐答案
using (bmp = new MemoryStream())
导致bmp
对象最终使用块被破坏.然后返回被破坏的bmp
变量.
using (bmp = new MemoryStream())
causes bmp
object is destroyed on end using block. And You return bmp
variable which is destroyed.
使用以下方法删除:
private Stream StreamFromBitmapSource(BitmapSource writeBmp)
{
Stream bmp = new MemoryStream();
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(writeBmp));
enc.Save(bmp);
return bmp;
}
这篇关于将BitmapSource转换为MemoryStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!