c#-4.0 - 这个错误怎么可能?使用 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
解码图像。
有人对如何解决此错误有任何想法吗?
谢谢。
解决方案
好的,我创建了一个解决方案。
在他的代码中,我做了以下事情:
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 应用程序中看不到的图像上崩溃。确实,我仍然看不到图像,但应用程序不再崩溃。
推荐阅读
- javascript - 同时调用Javascript函数和django视图函数
- flutter - 如何在 Flutter 中显示来自 Wordpress 的图像
- jquery - 具有自动生成 ID 的动态点击功能
- python - 如果使用不允许的类型调用 add_argument() 是否可能引发错误?
- amazon-s3 - React-admin :一次将多张图片上传到 s3
- azure-cognitive-search - 访问 Azure 认知搜索索引时出现套接字错误
- python - 更改yolov3安装路径 | 简历2
- python - 如何防止django中的多用户登录(注意:当第一个用户登录时,防止第二个用户登录)
- android - “返回 super.getItemId(position)”和只返回“返回位置”有什么区别
- macos - macOS:打印时自动打开应用程序