/// <summary> /// Validate if PostedFile is an Image and Provide Content /// </summary> /// <param name="postedFile"></param> /// <param name="fileData"></param> /// <param name="fileName"></param> /// <param name="fileType"></param> /// <param name="fileImage"></param> /// <returns>Posted File Information</returns> public static bool IsImage(this HttpPostedFile postedFile, out byte[] fileData, out string fileName, out string fileType, out Image fileImage, out string exception) { // outs are defined at validation fileImage = null; fileData = null; fileName = null; fileType = null; //------------------------------------------- // Check the image mime types //------------------------------------------- if (!string.Equals(postedFile.ContentType, "image/jpg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/jpeg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/png", StringComparison.OrdinalIgnoreCase)) { exception = "Ungültiger Dateityp"; return(false); } //------------------------------------------- // Check the image extension //------------------------------------------- var postedFileExtension = Path.GetExtension(postedFile.FileName); if (!string.Equals(postedFileExtension, ".jpg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFileExtension, ".png", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFileExtension, ".jpeg", StringComparison.OrdinalIgnoreCase)) { exception = "Ungültiger Dateityp"; return(false); } else { fileName = postedFile.FileName.Replace(postedFileExtension, ""); fileType = postedFileExtension; } //------------------------------------------- // Attempt to read the file and check the first bytes //------------------------------------------- try { if (!postedFile.InputStream.CanRead) { exception = "Datei konnte nicht gelesen werden"; return(false); } //------------------------------------------ // Check whether the image size exceeding the limit or not //------------------------------------------ if (postedFile.ContentLength < 512) { exception = "Datei zu klein, ungültige Datei?"; return(false); } fileData = postedFile.LoadUploadedFile(); string content = System.Text.Encoding.UTF8.GetString(fileData); if (Regex.IsMatch(content, @"<script|<html|<head|<title|<body|<pre|<table|<a\s+href|<img|<plaintext|<cross\-domain\-policy", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline)) { exception = "Ungültige Datei."; return(false); } } catch (Exception) { exception = "Unbehandelter Fehler"; return(false); } //------------------------------------------- // Try to instantiate new Bitmap, if .NET will throw exception // we can assume that it's not a valid image //------------------------------------------- try { fileImage = fileData.StreamToImage(); var temp = RotateImageByExifOrientationData(fileImage, true); // We don't need to reassign this when the Image wasn't being rotated anyways. if (RotateFlipType.RotateNoneFlipNone != temp) { fileData = ImageToByteArray(fileImage); } } catch (Exception e1) { exception = "Datei konnte nicht gelesen werden"; return(false); } finally { postedFile.InputStream.Position = 0; } exception = null; return(true); }