Graphics.drawimage Out Of Memory Error
Contents |
here for a quick overview of the site Help c# graphics.fromimage out of memory Center Detailed answers to any questions you might have Meta
System.drawing.graphics.drawimage Outofmemoryexception
Discuss the workings and policies of this site About Us Learn more about Stack
System Drawing Image Fromfile Out Of Memory
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Graphics.DrawImage: Out of memory exception up vote 5 down vote favorite 1 I just can't for the life of me figure out why I'm getting an out of memory exception here, even after much research on various websites and forums. Is anyone able to shed some light on the root of all evil in this code? The exception is thrown by the call to the Graphics.DrawImage() method, line 79. [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditImage(FileModel model) { var fileData = new MySite.Models.File(model.FileID, model.ClientID, ConfigurationManager.ConnectionStrings["MySite"].ConnectionString).Data; Image image = null; using (var memStream = new MemoryStream()) { memStream.Write(fileData, 0, fileData.Length); image = Image.FromStream(memStream); } using (var graphics = Graphics.FromImage(image)) { graphics.DrawImage(image, model.x1, model.y1, (model.x2 - model.x1), (model.y2 - model.y1)); graphics.Save(); } using (var memStream = new MemoryStream()) { image.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg); return File(memStream.ToArray(), "image/jpeg"); } } Stack trace: [OutOfMemoryException: Out of memory.] System.Drawing.Graphics.CheckErrorStatus(Int32 status) +1143476 System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) +141 ProPilot.Controllers.DocumentsController.EditImage(FileModel model) in C:\DEV\Web\
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up g.DrawImage throws out of memory exception up vote 1 down vote http://stackoverflow.com/questions/16055667/graphics-drawimage-out-of-memory-exception favorite In my aplication I am working with about 30 - 70 large images. By working I mean loading, cropping, resizing and adding to aviStream. Each image can have up to 4500p and 10MB (working with such large images gives me most of the out of memory exceptions). While testing I sometimes use smaller ones (2500p ~ 500kB each) and the memory situation is much better. I have http://stackoverflow.com/questions/29701758/g-drawimage-throws-out-of-memory-exception changed the architecture of my program to: load one image, process the needed operations, add it to the stream, close it. And doing the same with next image. But due to the complexity and size of the code, I am not 100% sure that I dispose and close every bitmap and image created for temporary purposes during this proces. I have the following error: An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll Usually when cropping image: public static Image Crop(Image imgPhoto, Point xy, int width, int height) { Rectangle cropRect = new Rectangle(xy, new Size(width, height)); Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(imgPhoto, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } return target; } The exception is thrown based on the cropping level, if I crop small part of an large image, lets say 800x600 pixels in the middle, it is better. If a crop in the oposite way, lets say create an 3500x2500 image where the original image is smaller (blank space will be automaticaly filled with selected color), I have the exception most of the time. One interesting thing is also that the exception is thrown around 23rd image, so previous images are procesed f
Forum C# Programming C-Sharp Programming [RESOLVED] g.DrawImage Out of memory If this is your first visit, be sure to check out the http://forums.codeguru.com/showthread.php?463819-RESOLVED-g-DrawImage-Out-of-memory FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed. To start viewing messages, select the forum https://siderite.blogspot.com/2009/09/outofmemoryexception-in.html that you want to visit from the selection below. Results 1 to 8 of 8 Thread: [RESOLVED] g.DrawImage Out of memory Tweet Thread Tools Show Printable Version Email this Page… Subscribe out of to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode October 23rd, 2008,03:43 PM #1 mnFlint View Profile View Forum Posts Junior Member Join Date Oct 2008 Posts 4 [RESOLVED] g.DrawImage Out of memory I need to write a program to reduce the size of some large images(13200x20394 1bpp png images). In my sample set of 10 I can out of memory process 4 before I get an "Out of memory" error. I am running 32bit windows XP pro with the maximum amount of ram installed. I get the error on the line that reads: g.DrawImage(.... Here is the code I am using to resize the image: private Bitmap ResizeBmp(Bitmap bm, int newWidth, int newHeight) { Bitmap newbmp = new Bitmap(newWidth, newHeight); try { Graphics g = Graphics.FromImage((Image)newbmp); try { //this is the line that throws the error g.DrawImage(bm, new Rectangle(0, 0, newWidth, newHeight), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel); } catch(Exception ex) { setError(ex.Message, -1); } g.Dispose(); } catch(Exception ex) { setError(ex.Message, -1); } return newbmp; } Why do I get a memory error like this? I have not seen problems with small png images. Thanks Reply With Quote October 23rd, 2008,06:16 PM #2 postmortem View Profile View Forum Posts Member Join Date Oct 2008 Posts 77 Re: g.DrawImage Out of memory I've experienced these problems when both pixel coordinates are the same for the rectangle. Reply With Quote October 24th, 2008,10:55 AM #3 mnFlint View Profile View Forum Posts Junior Member Join Date Oct 2008 P
got an Out of memory exception from a component that I had no control over. All images were icon size, 16x16 or a little more, so a lot of the explanations for the error based on "you don't have enough memory!" (duh!) were not helpful. The images did have transparent pixels, and my gut feeling is that it all came from there. I still don't know what caused it, but I did find a solution. Where I get the image I add an additional image = image.GetThumbnailImage(image.Width, image.Height, null, IntPtr.Zero); I know it's not something very intuitive, but it solved the problem. The only significant difference between the image before and after the thumbnailization is the PixelFormat property that changed from PixelFormat.Format8bppIndexed to PixelFormat.Format32bppArgb. The stack looks like this: at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback) at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr) And, using Reflector, I could go as far as pinpointing the error to the method System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectRectI which wraps the gdiplus.dll GdipDrawImageRectRectI function. It returns error status 3, which is Out of memory, and that is that. I couldn't find a C++ code for the GdiPlus library and even if I had, who I am kidding? :) Email This BlogThis! Share to Twitter Share to Facebook Share to Pinterest 7 comments: Jeff said... Thanks, I was having the exact same problem. I had embedded png icons 16x16 and when I changed the enabled property of their parent I got this obscure error. Your workaround has solved the problem. Is this a bug within the framework? 20 July, 2010 12:01 Siderite said... I can only assume it is a bug from GDI plus. At least there I lost the trace for the error. 20 July, 2010 13:09 Poet For Life said... hi,I used the Getthumbnail method , but the quality of image deterirates a lot and text on image is not readable , Please suggest what can be done 05 June, 2014 22:48 Siderite said... So you are resizing the image t