将JPEG图像转换为字节数组 - COM异常

时间:2021-11-27 21:23:26

Using C#, I'm trying to load a JPEG file from disk and convert it to a byte array. So far, I have this code:

使用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(); 
    }
}

However, executing line ARGH gives me the message:

但是,执行ARGH行给我的信息是:

COMException was unhandled. The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

COMException未处理。句柄无效。 (HRESULT异常:0x80070006(E_HANDLE))

I don't think there is anything special about the file Lenna.jpg - I downloaded it from http://computervision.wikia.com/wiki/File:Lenna.jpg. Can you tell what is wrong with the above code?

我不认为文件Lenna.jpg有什么特别之处 - 我是从http://computervision.wikia.com/wiki/File:Lenna.jpg下载的。你能说出上面的代码有什么问题吗?

4 个解决方案

#1


38  

Check the examples from this article: http://www.codeproject.com/KB/recipes/ImageConverter.aspx

查看本文中的示例:http://www.codeproject.com/KB/recipes/ImageConverter.aspx

Also it's better to use classes from System.Drawing

最好使用System.Drawing中的类

Image img = Image.FromFile(@"C:\Lenna.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    arr =  ms.ToArray();
}

#2


10  

Other suggestion:

其他建议:

byte[] image = System.IO.File.ReadAllBytes ( Server.MapPath ( "noimage.png" ) );

Should be working not only with images.

应该不仅适用于图像。

#3


4  

public byte[] imageToByteArray(System.Drawing.Image imageIn)  
{   
 MemoryStream ms = new MemoryStream();     

 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);   
 return  ms.ToArray();   
}

#4


3  

The reason this error happens is because the BitmapFrame.Create() method you are using defaults to an OnDemand load. The BitmapFrame doesn't try to read the stream it's associated with until the call to encoder.Save, by which point the stream is already disposed.

发生此错误的原因是您使用的BitmapFrame.Create()方法默认为OnDemand加载。在调用encoder.Save之前,BitmapFrame不会尝试读取与之关联的流,此时流已经被释放。

You could either wrap the entire function in the using {} block, or use an alternative BitmapFrame.Create(), such as:

您可以将整个函数包装在using {}块中,也可以使用替代的BitmapFrame.Create(),例如:

BitmapFrame.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

#1


38  

Check the examples from this article: http://www.codeproject.com/KB/recipes/ImageConverter.aspx

查看本文中的示例:http://www.codeproject.com/KB/recipes/ImageConverter.aspx

Also it's better to use classes from System.Drawing

最好使用System.Drawing中的类

Image img = Image.FromFile(@"C:\Lenna.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    arr =  ms.ToArray();
}

#2


10  

Other suggestion:

其他建议:

byte[] image = System.IO.File.ReadAllBytes ( Server.MapPath ( "noimage.png" ) );

Should be working not only with images.

应该不仅适用于图像。

#3


4  

public byte[] imageToByteArray(System.Drawing.Image imageIn)  
{   
 MemoryStream ms = new MemoryStream();     

 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);   
 return  ms.ToArray();   
}

#4


3  

The reason this error happens is because the BitmapFrame.Create() method you are using defaults to an OnDemand load. The BitmapFrame doesn't try to read the stream it's associated with until the call to encoder.Save, by which point the stream is already disposed.

发生此错误的原因是您使用的BitmapFrame.Create()方法默认为OnDemand加载。在调用encoder.Save之前,BitmapFrame不会尝试读取与之关联的流,此时流已经被释放。

You could either wrap the entire function in the using {} block, or use an alternative BitmapFrame.Create(), such as:

您可以将整个函数包装在using {}块中,也可以使用替代的BitmapFrame.Create(),例如:

BitmapFrame.Create(fs, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);