From 70ebd7e12914d68bf06a3686b5583464213feb69 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Thu, 3 Jan 2013 15:20:13 -0100 Subject: [PATCH] U4-335 Fixed out of memory exception in cropper (checks in CropImage method) --- .../imagecropper/ImageManipulation.cs | 134 +++--------------- 1 file changed, 18 insertions(+), 116 deletions(-) diff --git a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs index 3536b3a037..b74f88c755 100644 --- a/src/umbraco.editorControls/imagecropper/ImageManipulation.cs +++ b/src/umbraco.editorControls/imagecropper/ImageManipulation.cs @@ -31,62 +31,25 @@ namespace umbraco.editorControls.imagecropper DirectoryInfo di = new DirectoryInfo(path); if (!di.Exists) di.Create(); - using(Image croppedImage = cropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight))) + using(Image croppedImage = CropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight))) { - using(Image resizedImage = resizeImage(croppedImage, new Size(sizeWidth, sizeHeight))) + using(Image resizedImage = ResizeImage(croppedImage, new Size(sizeWidth, sizeHeight))) { using (Bitmap b = new Bitmap(resizedImage)) { - saveJpeg(String.Format("{0}/{1}.jpg", path, name), b, quality); + SaveJpeg(String.Format("{0}/{1}.jpg", path, name), b, quality); } } } - - - //saveJpeg( - // String.Format("{0}/{1}.jpg", path, name), - // new Bitmap( - // resizeImage(cropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight)), new Size(sizeWidth, sizeHeight))), - // quality - // ); - - //using (FileStream stm = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) - //{ - //using (Image image = Image.FromStream(stm)) - //{ - - //} - //stm.Close(); - //} - - - //using (Image image = Image.FromFile(sourceFile)) - //{ - // //image = cropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight)); - // //cropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight)); - // //image = resizeImage(image, new Size(sizeWidth, sizeHeight)); - // //resizeImage(image, new Size(sizeWidth, sizeHeight)); - // string path = sourceFile.Substring(0, sourceFile.LastIndexOf('\\') + 1) + "Crops"; - // DirectoryInfo di = new DirectoryInfo(path); - // if (!di.Exists) di.Create(); - // saveJpeg( - // String.Format("{0}/{1}.jpg", path, name), - // new Bitmap( - // resizeImage(cropImage(image, new Rectangle(cropX, cropY, cropWidth, cropHeight)), new Size(sizeWidth, sizeHeight))), - // quality - // ); - - // image.Dispose(); - //} } - private static void saveJpeg(string path, Bitmap img, long quality) + private static void SaveJpeg(string path, Bitmap img, long quality) { // Encoder parameter for image quality EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); // Jpeg image codec - ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg"); + ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); if (jpegCodec == null) return; @@ -97,7 +60,7 @@ namespace umbraco.editorControls.imagecropper img.Save(path, jpegCodec, encoderParams); } - private static ImageCodecInfo getEncoderInfo(string mimeType) + private static ImageCodecInfo GetEncoderInfo(string mimeType) { // Get image codecs for all image formats ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); @@ -109,39 +72,22 @@ namespace umbraco.editorControls.imagecropper return null; } - private static Image cropImage(Image img, Rectangle cropArea) + private static Image CropImage(Image img, Rectangle cropArea) { - Bitmap bmpImage = new Bitmap(img); - Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat); - return (Image)(bmpCrop); + var bmpImage = new Bitmap(img); + + if (cropArea.Right > img.Width) + cropArea.Width -= (cropArea.Right - img.Width); + + if (cropArea.Bottom > img.Height) + cropArea.Height -= (cropArea.Bottom - img.Height); + + var bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat); + return bmpCrop; } - private static Image resizeImage(Image imgToResize, Size size) + private static Image ResizeImage(Image imgToResize, Size size) { - //int sourceWidth = imgToResize.Width; - //int sourceHeight = imgToResize.Height; - - //float nPercent = 0; - //float nPercentW = 0; - //float nPercentH = 0; - - //nPercentW = ((float)size.Width / (float)sourceWidth); - //nPercentH = ((float)size.Height / (float)sourceHeight); - - //if (nPercentH < nPercentW) - // nPercent = nPercentH; - //else - // nPercent = nPercentW; - - //int destWidth = (int)(sourceWidth * nPercent); - //int destHeight = (int)(sourceHeight * nPercent); - - - - - - - int destWidth = size.Width; int destHeight = size.Height; @@ -163,50 +109,6 @@ namespace umbraco.editorControls.imagecropper g.Dispose(); return b; - - - - - -#if false - - int destWidth = size.Width; - int destHeight = size.Height; - - using (Bitmap b = new Bitmap(destWidth, destHeight)) - { - using (Graphics g = Graphics.FromImage(b)) - { - g.PixelOffsetMode = PixelOffsetMode.HighQuality; - using (ImageAttributes ia = new ImageAttributes()) - { - ia.SetWrapMode(WrapMode.TileFlipXY); - g.Clear(Color.White); - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - g.CompositingQuality = CompositingQuality.HighQuality; - g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawImage(imgToResize, new Rectangle(0, 0, destWidth, destHeight), 0, 0, imgToResize.Width, - imgToResize.Height, GraphicsUnit.Pixel, ia); - } - } - return b; - } - -#endif - -#if false - int destWidth = size.Width; - int destHeight = size.Height; - - Bitmap b = new Bitmap(destWidth, destHeight); - Graphics g = Graphics.FromImage((Image)b); - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - - g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); - g.Dispose(); - - return (Image)b; -#endif } } } \ No newline at end of file