怎么会出现这种错误?使用Imazen.WebP.SimpleDecoder检测到无效的WebP头。

问题描述 投票:0回答:1

我有以下代码。

SimpleDecoder decoder = new SimpleDecoder();
byte[] fileBytes = File.ReadAllBytes(filename);
Bitmap image = decoder.DecodeFromBytes(fileBytes, fileBytes.Length);

PictureBox pb = new PictureBox
    {
    Size = new Size(100, 100),
    Image = image,
    SizeMode = PictureBoxSizeMode.Zoom,
    Tag = filename
    };

FlpThumbnails.Controls.Add(pb);
pb.Refresh();
Application.DoEvents();

在... Bitmap image = decoder.DecodeFromBytes(fileBytes, fileBytes.Length); 我得到的异常是 System.Exception: 'Invalid WebP header detected'. 然而,在我的Windows资源管理器中,我可以很好地看到图像。

我使用的是 Imazen.WebP.SimpleDecoder 来解码图像。

谁有什么办法可以解决这个错误?

谢谢,我有以下代码

c#-4.0 webp
1个回答
0
投票

好吧,我创建了一个解决方案。

在他的代码中,我做了以下工作。

public static Bitmap DecodeFromPointer(IntPtr data, long length)
    {
        int w = 0, h = 0;
        //Validate header and determine size
        if (NativeMethods.WebPGetInfo(data, (UIntPtr)length, ref w, ref h) == 0)
        {
            w = 100;
            h = 100;
            NativeMethods.WebPGetInfo(data, (UIntPtr)length, ref w, ref h);
            //  throw new Exception("Invalid WebP header detected");
        }

我注释了 throw new 异常,并添加了我使用的默认缩略图大小100,100。它仍然不显示图像,但它继续,直到... ...

再往下一点,发现了这个。

//  if (bd.Scan0 != result) throw new Exception("Failed to decode WebP image with error " + (long)result);

正如你所看到的,我只是简单地把这一行也注释掉了。

这就是我所要做的一切,以获得 Imazen.WebP.SimpleDecoder 在资源管理器中可以看到但在.Net应用程序中看不到的图像上不崩溃。的确,我仍然看不到图像,但应用程序不会再崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.