private bool ProcessImageStream(Stream fileContent, string extension) { Image image = null; var imageChecker = new ImageChecker(); CheckResult = imageChecker.CheckStream(fileContent, true, out image); try { FileSize = imageChecker.DataSize; ImageWidth = imageChecker.Width; ImageHeight = imageChecker.Height; if (CheckResult != ImageCheckResult.JPEGImage && CheckResult != ImageCheckResult.GIFImage && CheckResult != ImageCheckResult.PNGImage) { ErrorMessage = imageChecker.FormatErrorMessage(CheckResult); return(false); } else { IsImage = true; extension = (CheckResult == ImageCheckResult.PNGImage ? ".png" : (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg")); var temporaryPath = UploadHelper.TemporaryPath; Directory.CreateDirectory(temporaryPath); TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath); string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N")); FilePath = baseFileName + extension; fileContent.Seek(0, SeekOrigin.Begin); using (FileStream fs = new FileStream(FilePath, FileMode.Create)) fileContent.CopyTo(fs); if (ThumbWidth > 0 || ThumbHeight > 0) { using (System.Drawing.Image thumbImage = ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor)) { ThumbFile = baseFileName + "_t.jpg"; if (ThumbQuality != 0) { var p = new System.Drawing.Imaging.EncoderParameters(1); p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality); ImageCodecInfo jpegCodec = null; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = 0; i < codecs.Length; i++) { if (codecs[i].MimeType == "image/jpeg") { jpegCodec = codecs[i]; } } thumbImage.Save(ThumbFile, jpegCodec, p); } else { thumbImage.Save(ThumbFile, System.Drawing.Imaging.ImageFormat.Jpeg); } ThumbHeight = thumbImage.Width; ThumbWidth = thumbImage.Height; } } return(true); } } finally { if (image != null) { image.Dispose(); image = null; } } }
private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer) { var imageChecker = new ImageChecker(); CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger); try { FileSize = imageChecker.DataSize; ImageWidth = imageChecker.Width; ImageHeight = imageChecker.Height; if (CheckResult != ImageCheckResult.JPEGImage && CheckResult != ImageCheckResult.GIFImage && CheckResult != ImageCheckResult.PNGImage) { ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer); return(false); } else { IsImage = true; var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" : (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg"); storage.PurgeTemporaryFiles(); if (ThumbWidth > 0 || ThumbHeight > 0) { using Image thumbImage = ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor); var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile); using (var ms = new MemoryStream()) { if (ThumbQuality != 0) { var p = new EncoderParameters(1); p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality); ImageCodecInfo jpegCodec = null; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = 0; i < codecs.Length; i++) { if (codecs[i].MimeType == "image/jpeg") { jpegCodec = codecs[i]; } } thumbImage.Save(ms, jpegCodec, p); } else { thumbImage.Save(ms, ImageFormat.Jpeg); } ms.Seek(0, SeekOrigin.Begin); ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false); } ThumbHeight = thumbImage.Width; ThumbWidth = thumbImage.Height; } return(true); } } finally { if (image != null) { image.Dispose(); } } }
private bool ProcessImageStream(Stream fileContent, string extension) { Image image = null; var imageChecker = new ImageChecker(); CheckResult = imageChecker.CheckStream(fileContent, true, out image); try { FileSize = imageChecker.DataSize; ImageWidth = imageChecker.Width; ImageHeight = imageChecker.Height; if (CheckResult != ImageCheckResult.JPEGImage && CheckResult != ImageCheckResult.GIFImage && CheckResult != ImageCheckResult.PNGImage) { ErrorMessage = imageChecker.FormatErrorMessage(CheckResult); return false; } else { IsImage = true; extension = (CheckResult == ImageCheckResult.PNGImage ? ".png" : (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg")); var temporaryPath = UploadHelper.TemporaryPath; Directory.CreateDirectory(temporaryPath); TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath); string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N")); FilePath = baseFileName + extension; fileContent.Seek(0, SeekOrigin.Begin); using (FileStream fs = new FileStream(FilePath, FileMode.Create)) fileContent.CopyTo(fs); if (ThumbWidth > 0 || ThumbHeight > 0) { using (System.Drawing.Image thumbImage = ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor)) { ThumbFile = baseFileName + "_t.jpg"; if (ThumbQuality != 0) { var p = new System.Drawing.Imaging.EncoderParameters(1); p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality); ImageCodecInfo jpegCodec = null; ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = 0; i < codecs.Length; i++) if (codecs[i].MimeType == "image/jpeg") jpegCodec = codecs[i]; thumbImage.Save(ThumbFile, jpegCodec, p); } else thumbImage.Save(ThumbFile, System.Drawing.Imaging.ImageFormat.Jpeg); ThumbHeight = thumbImage.Width; ThumbWidth = thumbImage.Height; } } return true; } } finally { if (image != null) { image.Dispose(); image = null; } } }
public static void CheckUploadedImageAndCreateThumbs(ImageUploadEditorAttribute attr, ref string temporaryFile) { ImageCheckResult[] supportedFormats = null; if (!attr.AllowNonImage) supportedFormats = new ImageCheckResult[] { ImageCheckResult.JPEGImage, ImageCheckResult.GIFImage, ImageCheckResult.PNGImage }; UploadHelper.CheckFileNameSecurity(temporaryFile); var checker = new ImageChecker(); checker.MinWidth = attr.MinWidth; checker.MaxWidth = attr.MaxWidth; checker.MinHeight = attr.MinHeight; checker.MaxHeight = attr.MaxHeight; checker.MaxDataSize = attr.MaxSize; Image image = null; try { var temporaryPath = UploadHelper.DbFilePath(temporaryFile); using (var fs = new FileStream(temporaryPath, FileMode.Open)) { if (attr.MinSize != 0 && fs.Length < attr.MinSize) throw new ValidationError(String.Format(Texts.Controls.ImageUpload.UploadFileTooSmall, UploadHelper.FileSizeDisplay(attr.MinSize))); if (attr.MaxSize != 0 && fs.Length > attr.MaxSize) throw new ValidationError(String.Format(Texts.Controls.ImageUpload.UploadFileTooBig, UploadHelper.FileSizeDisplay(attr.MaxSize))); ImageCheckResult result; if (Path.GetExtension(temporaryFile).ToLowerInvariant() == ".swf") { result = ImageCheckResult.FlashMovie; // validate swf file somehow! } else { result = checker.CheckStream(fs, true, out image); } if (result == ImageCheckResult.InvalidImage && attr.AllowNonImage) { return; } if (result > ImageCheckResult.UnsupportedFormat || (supportedFormats != null && Array.IndexOf(supportedFormats, result) < 0)) { string error = checker.FormatErrorMessage(result); throw new ValidationError(error); } if (result >= ImageCheckResult.FlashMovie) return; string basePath = UploadHelper.TemporaryPath; string baseFile = Path.GetFileNameWithoutExtension(Path.GetFileName(temporaryPath)); TemporaryFileHelper.PurgeDirectoryDefault(basePath); if ((attr.ScaleWidth > 0 || attr.ScaleHeight > 0) && ((attr.ScaleWidth > 0 && (attr.ScaleSmaller || checker.Width > attr.ScaleWidth)) || (attr.ScaleHeight > 0 && (attr.ScaleSmaller || checker.Height > attr.ScaleHeight)))) { using (Image scaledImage = ThumbnailGenerator.Generate( image, attr.ScaleWidth, attr.ScaleHeight, attr.ScaleMode, Color.Empty)) { temporaryFile = baseFile + ".jpg"; fs.Close(); scaledImage.Save(Path.Combine(basePath, temporaryFile), System.Drawing.Imaging.ImageFormat.Jpeg); temporaryFile = "temporaryFile/" + temporaryFile; } } var thumbSizes = attr.ThumbSizes.TrimToNull(); if (thumbSizes == null) return; foreach (var sizeStr in thumbSizes.Replace(";", ",").Split(new char[] { ',' })) { var dims = sizeStr.ToLowerInvariant().Split(new char[] { 'x' }); int w, h; if (dims.Length != 2 || !Int32.TryParse(dims[0], out w) || !Int32.TryParse(dims[1], out h) || w < 0 || h < 0 || (w == 0 && h == 0)) throw new ArgumentOutOfRangeException("thumbSizes"); using (Image thumbImage = ThumbnailGenerator.Generate(image, w, h, attr.ThumbMode, Color.Empty)) { string thumbFile = Path.Combine(basePath, baseFile + "_t" + w.ToInvariant() + "x" + h.ToInvariant() + ".jpg"); thumbImage.Save(thumbFile); } } } } finally { if (image != null) image.Dispose(); } }
private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer) { var imageChecker = new ImageChecker(); CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger); try { FileSize = imageChecker.DataSize; ImageWidth = imageChecker.Width; ImageHeight = imageChecker.Height; if (CheckResult != ImageCheckResult.JPEGImage && CheckResult != ImageCheckResult.GIFImage && CheckResult != ImageCheckResult.PNGImage) { ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer); return(false); } else { IsImage = true; var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" : (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg"); storage.PurgeTemporaryFiles(); if (ThumbWidth > 0 || ThumbHeight > 0) { ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor, inplace: true); var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile); using (var ms = new MemoryStream()) { if (ThumbQuality != 0) { image.Save(ms, new JpegEncoder { Quality = ThumbQuality }); } else { image.Save(ms, new JpegEncoder()); } ms.Seek(0, SeekOrigin.Begin); ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false); } ThumbHeight = image.Width; ThumbWidth = image.Height; } return(true); } } finally { if (image != null) { image.Dispose(); } } }