判断文件是一个图像 [英] determine if file is an image

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

问题描述

我通过目录循环和复制所有文件。现在我做string.EndsWith检查.JPG或巴纽等。

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

有没有确定一个文件是一个图像(任何图像类型),而上述哈克检查的更优雅的方式?

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

推荐答案

检查文件的已知的头

第一个8字节的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天全站免登陆