如何在 C# 中验证图像文件格式 [英] How to validate image file format in C#

查看:18
本文介绍了如何在 C# 中验证图像文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道验证给定图像的文件格式的脚本.目前我正在填充一个图像对象,查看它的高度、宽度和分辨率.我没有看到这个对象有任何解释文件格式的特定属性.

Does anyone know the script to validate what the file format is for a given image. Currently i am populating an image object, looking at it's height, width, and resolution. I don't see any specific properties off of this object that explains the file format.

我想检查 jpg、AI、PSD、High Jes Jpg、Bitmap 和 Tiff.

I would like to check for jpg, AI, PSD, High Jes Jpg, Bitmap, and Tiff.

这是我当前的脚本:

        protected bool IsValidImage(HttpPostedFileBase file, string fileName) {

        //verify that the image is no more than 648 wide and 648 pixels tall
        Image imgPhoto = Image.FromStream(file.InputStream);
        if (imgPhoto.Width > 648)
            return false;
        if (imgPhoto.Height > 648)
            return false;
        if (imgPhoto.HorizontalResolution != 72 || imgPhoto.VerticalResolution != 72)
            return false;
        return true;

    }

提前致谢

推荐答案

使用 Image.RawFormat.结果是 ImageFormat 类的一个实例,可以与 ImageFormat 的静态属性进行比较.

Use Image.RawFormat. The result is an instance of the ImageFormat class which can be compared against the static properties of ImageFormat.

请参阅ImageFormat 类属性了解更多详情.

See the ImageFormat class properties for more details.

这篇关于如何在 C# 中验证图像文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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