Converting a JPEG image to a byte array - COM exception

后端 未结 4 2037
孤城傲影
孤城傲影 2020-12-29 23:47

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:

static void Main(string[] args)
{
    System.Wi         


        
4条回答
  •  清酒与你
    2020-12-29 23:58

    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.

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

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

提交回复
热议问题