[英]How to validate image file format in C#
有沒有人知道腳本來驗證給定圖像的文件格式是什么。 目前我正在填充圖像對象,查看它的高度,寬度和分辨率。 我沒有看到解釋文件格式的此對象的任何特定屬性。
我想查看jpg,AI,PSD,High Jes Jpg,Bitmap和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
的靜態屬性進行比較。
有關更多詳細信息,請參閱ImageFormat
類屬性 。
public bool validateImage(byte[] bytes)
{
try
{
Stream stream = new MemoryStream(bytes);
using(Image img = Image.FromStream(stream))
{
if (img.RawFormat.Equals(ImageFormat.Bmp) ||
img.RawFormat.Equals(ImageFormat.Gif) ||
img.RawFormat.Equals(ImageFormat.Jpeg) ||
img.RawFormat.Equals(ImageFormat.Png))
return true;
}
return false;
}
catch
{
return false;
}
}
您可以訪問Wotsit以找出在文件開頭用作標記的魔術字節。 單擊“圖形文件”以查看文件格式列表。
關於什么:
bool isJpeg = imgPhoto.RawFormat.Equals(Imaging.ImageFormat.Jpeg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.