【发布时间】:2011-11-16 19:27:26
【问题描述】:
使用 C#,我正在尝试从磁盘加载 JPEG 文件并将其转换为字节数组。到目前为止,我有这个代码:
static void Main(string[] args)
{
System.Windows.Media.Imaging.BitmapFrame bitmapFrame;
using (var fs = new System.IO.FileStream(@"C:\Lenna.jpg", FileMode.Open))
{
bitmapFrame = BitmapFrame.Create(fs);
}
System.Windows.Media.Imaging.BitmapEncoder encoder =
new System.Windows.Media.Imaging.JpegBitmapEncoder();
encoder.Frames.Add(bitmapFrame);
byte[] myBytes;
using (var memoryStream = new System.IO.MemoryStream())
{
encoder.Save(memoryStream); // Line ARGH
// mission accomplished if myBytes is populated
myBytes = memoryStream.ToArray();
}
}
但是,执行ARGH 行给了我消息:
COMException 未处理。句柄无效。 (例外来自 HRESULT: 0x80070006 (E_HANDLE))
我认为文件Lenna.jpg 没有什么特别之处——我是从http://computervision.wikia.com/wiki/File:Lenna.jpg 下载的。你能说出上面的代码有什么问题吗?
【问题讨论】: