首页 > 解决方案 > 这个错误怎么可能?使用 Imazen.WebP.SimpleDecoder '检测到无效的 WebP 标头'

问题描述

我有以下代码:

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.0webp

解决方案


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

在他的代码中,我做了以下事情:

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,以免在我可以在 Explorer 中看到但在我的 .Net 应用程序中看不到的图像上崩溃。确实,我仍然看不到图像,但应用程序不再崩溃。


推荐阅读