如何在C#/ .Net中验证JPEG图像未损坏

时间:2023-01-24 21:22:37

I need to be able to validate if a JPEG image is valid and not corrupted. This is not the same as question 210650 which just asks wants to validate that it is, in fact, a JPEG image. Loading from Image with validation eg:

我需要能够验证JPEG图像是否有效且未损坏。这与问题210650不同,问题210650只是要求验证它实际上是JPEG图像。从图像加载验证,例如:

var testJpg = 
  Bitmap.FromStream(filename, useEmbeddedColorManagement, validateData);

will not throw an exception for clipped images. Currently our solution is to try to open the file an external GD script and parse for any errors thrown from gdImageCreateFromJpeg.

不会为剪裁的图像抛出异常。目前我们的解决方案是尝试打开文件外部GD脚本并解析从gdImageCreateFromJpeg引发的任何错误。

gdImagePtr im;
im = gdImageCreateFromJpeg(testFile);
fclose(in);    

But there has to be some .Net code somewhere that validates that a JPEG is not only valid, but contains the complete image?

但必须有一些.Net代码可以验证JPEG不仅有效,而且包含完整的图像?

2 个解决方案

#1


Taking JPEG reference structure from Wikipedia, maybe you can look for EOI byte from the stream?

从*中获取JPEG参考结构,也许您可​​以从流中查找EOI字节?

#2


There is the pnetlib that provides an interop to jpeglib using c#, once you have that you can validate whatever you want.
You can try to use thier JpegReader, maybe it will give you an error out of the box.

有一个pnetlib使用c#为jpeglib提供了一个互操作,一旦你有了,你可以验证你想要的任何东西。你可以尝试使用他们的JpegReader,也许它会给你一个开箱即用的错误。

#1


Taking JPEG reference structure from Wikipedia, maybe you can look for EOI byte from the stream?

从*中获取JPEG参考结构,也许您可​​以从流中查找EOI字节?

#2


There is the pnetlib that provides an interop to jpeglib using c#, once you have that you can validate whatever you want.
You can try to use thier JpegReader, maybe it will give you an error out of the box.

有一个pnetlib使用c#为jpeglib提供了一个互操作,一旦你有了,你可以验证你想要的任何东西。你可以尝试使用他们的JpegReader,也许它会给你一个开箱即用的错误。