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

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

问题描述

有谁知道脚本来验证什么文件格式是给定的图像。目前,我填充图像对象,看着它的高度,宽度和分辨率。我没有看到任何具体的属性了这个对象,解释文件格式。



我想检查JPG,AI,PSD,高赫苏斯JPG,位图,和TIFF



下面是我目前的脚本:

 保护布尔IsValidImage(HttpPostedFileBase文件,字符串文件名){

//验证图像是不超过648宽和648象素高
图片imgPhoto = Image.FromStream(file.InputStream);
如果(imgPhoto.Width> 648)
返回FALSE;
如果(imgPhoto.Height> 648)
返回FALSE;
如果(imgPhoto.Horizo​​ntalResolution = 72 || imgPhoto.VerticalResolution = 72!)
返回FALSE;
返回真;
$ B提前


$ B}



感谢< DIV CLASS =h2_lin>解决方案

使用 Image.RawFormat 。其结果是的imageformat 类,它可以针对的imageformat 的静态属性进行比较的一个实例。



请参阅 HTTP :了解更多详情//msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat.aspx


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.

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

here is my current script:

        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;

    }

Thanks in advance

解决方案

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

See http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat.aspx for more details.

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

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