如何检查是否JPEG图像文件实际上是试图加载前一个有效的图像? [英] How to check if a jpeg image file is actually a valid image before trying to load?

查看:177
本文介绍了如何检查是否JPEG图像文件实际上是试图加载前一个有效的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#.NET 4.0 的基础上,一个问题,我曾与下载假象(保存错误aspx页面作为image.jpg的,而不是图像的实际图像。 JPG),我需要以某种方式读取该文件,并确定它是否是一个有效的图像或没有。我只需要1功能公共BOOL IsValidJpgImage(字符串映像文件名称); 凡是返回false(是不是有效的图像文件),我会从磁盘上删除

In C#.NET 4.0, based on a problem I had with downloading false images (saving an error aspx page as image.jpg instead of an actual image in image.jpg), I need to somehow read the file and identify if it is a valid image or not. I just need 1 function public bool IsValidJpgImage(string ImageFilename); Anything that returns false (is not a valid image file) I will delete from the disk.

推荐答案

据我因为我知道所有的JPEG文件与相同的10个字符的ASCII字符串内部开始,虽然我不记得前6个字符是什么。这是蔡作馨,即使文件扩展名是错误的JPEG图像的快速/肮脏的方法。

As far I as I know all JPEGs begin internally with same 10 character ASCII string although I can't remember what the first 6 characters are. This is quick/dirty method of indentifying a jpeg image even if the file extension is wrong.

grep -P '^......JFIF' ./'raccoon paint.jpeg'

如果图像不是JPEG比赛将失败。

If the image is not a jpeg the match will fail.

这篇关于如何检查是否JPEG图像文件实际上是试图加载前一个有效的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆