Memory Exception Using WebImage.Resize

I am using WebImage in MVC3 to resize an image. Basically, the goal of this is to create a thumbnail image of the uploaded file. I will not control how large the files are initially, so I need to create a file thumbnail to speed up the preview site.

I have some files that need to be downloaded and have a size of about 4 MB, which is not a problem when downloading. The problem I am facing is to create a thumbnail. I upload the file first, and after saving it to the server, I create a new WebImage object for the thumbnail.

 // Save a thumbnail of the file WebImage image = new WebImage(savedFileName); // Resize the image image.Resize(135, 150, true); // Save the thumbnail image.Save(FileName); // <<--- Out of memory exception here // Dispose of the image image = null; 

When I try to save a file, I get an exception in memory. Any ideas on how I can solve this?

+6
c # asp.net-mvc-3
source share
5 answers

Due to an error in WebImage I had to resort to the following code:

 // Save a thumbnail of the file byte[] fileBytes = System.IO.File.ReadAllBytes(savedFileName); System.Drawing.Image i; using (MemoryStream ms = new MemoryStream()) { ms.Write(fileBytes, 0, fileBytes.Length); i = System.Drawing.Image.FromStream(ms); } // Create the thumbnail System.Drawing.Image thumbnail = i.GetThumbnailImage(135, 150, () => false, IntPtr.Zero); 
+8
source share

I came across this myself using WebImage Resize, and found that the problem is with the JPG image, which is actually a CMYK image. Changing the image mode in PS, RGB not only reduced the file size that was used by the WebImage resizing method, but also performed much faster.

For use on the Internet, I try to ensure that all JPG images are RGB and saved in the basic format (as opposed to progressive). This prevents a lot of errors and errors.

+3
source share

I got an OutOfMemoryException from WebImage.Save () when I try to resize and save a JPG image using CMYK color space.

The workaround I found was to save the image to disk and reload it before resizing.

 var logoWebImage = new WebImage(newLogo.InputStream); // Start workaround: Save and reload to avoid OutOfMemoryException when image color space is CMYK. logoWebImage.Save(filePath: DataFilePaths.LogoImageFile, imageFormat: "png"); logoWebImage = new WebImage(DataFilePaths.LogoImageFile); // End workaround logoWebImage.Resize(300, 300, preserveAspectRatio: true, preventEnlarge: true); logoWebImage.Save(filePath: DataFilePaths.LogoImageFile, imageFormat: "png"); 

This is an ugly workaround, but it is especially necessary when the input image is a logo. Because designers often provide images of logos that use the CMYK color space, because it's better for printing. A.

+3
source share

OK, this looks like an error in MVC3. The error you get is just a standard GDI + error when GDI + tries to access pixels in places where it does not exist.

I believe the problem is rounding the pixels when calculating the aspect ratio , so I think that if you change 135, 150 to, for example, 136, 151 , this will work.

Perhaps I thought about finding an error in my code and posting them.


UPDATE - Possible Workaround

Try passing true for the 4th parameter:

 // Resize the image image.Resize(135, 150, true, true); 

I really see the error in the code:

  if (num3 > num4) { height = (int) Math.Round((double) ((num4 * image.Height) / 100.0)); } else if (num3 < num4) { width = (int) Math.Round((double) ((num3 * image.Width) / 100.0)); } 
0
source share

I ended this up as a workaround:

  public static Image ScaleImage(string fileName, int maxWidth, int maxHeight) { var image = Image.FromFile(fileName); var ratioX = (double)maxWidth / image.Width; var ratioY = (double)maxHeight / image.Height; var ratio = Math.Min(ratioX, ratioY); var newWidth = (int)(image.Width * ratio); var newHeight = (int)(image.Height * ratio); var newImage = new Bitmap(newWidth, newHeight); var g = Graphics.FromImage(newImage); g.Clear(Color.White); // matters for transparent images g.DrawImage(image, 0, 0, newWidth, newHeight); return newImage; } 
0
source share

All Articles