确定文件是否为图像 [英] determine if file is an image

查看:29
本文介绍了确定文件是否为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历一个目录并复制所有文件.现在我正在对 ".jpg"".png" 等进行 string.EndsWith 检查..

I am looping through a directory and copying all files. Right now I am doing string.EndsWith checks for ".jpg" or ".png", etc . .

是否有更优雅的方法来确定文件是否为图像(任何图像类型)而无需像上面那样进行 hacky 检查?

Is there any more elegant way of determining if a file is an image (any image type) without the hacky check like above?

推荐答案

检查文件中的 已知标题.(此答案中也提到了来自链接的信息)

Check the file for a known header. (Info from link also mentioned in this answer)

PNG 文件的前八个字节始终包含以下(十进制)值:137 80 78 71 13 10 26 10

The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10

这篇关于确定文件是否为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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