Image Save Error
Contents |
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 a generic error occurred in gdi c# image save about Stack Overflow the company Business Learn more about hiring developers or posting a generic error occurred in gdi bitmap save c# ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack system.runtime.interopservices.externalexception (0x80004005): a generic error occurred in gdi+. Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to find reason for Generic GDI+ error when saving an a generic error occurred in gdi in c# windows application image? up vote 7 down vote favorite 4 Having a code that works for ages when loading and storing images, I discovered that I have one single image that breaks this code: const string i1Path = @"c:\my\i1.jpg"; const string i2Path = @"c:\my\i2.jpg"; var i = Image.FromFile(i1Path); i.Save(i2Path, ImageFormat.Jpeg); The exception is: System.Runtime.InteropServices.ExternalException occurred A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename,
How To Solve A Generic Error Occurred In Gdi+
ImageFormat format) at ... As far as I can see, there is nothing special about the image. It is approx 250 pixels in size and can be opened in e.g. Windows Image Viewer or Paint.NET: (Since the image above, after being uploaded to Stack Overflow does not produce the error anymore, I've put the original image here) What I discovered is that upon calling the Save method, the destination image file is being created with zero bytes. I am really clueless on what causes the error. My questions: Can you think of any special thing that would hinder .NET from saving the image? Is there any way (beside panicing) to narrow down these kind of errors? c# .net image gdi+ system.drawing share|improve this question edited Mar 22 '13 at 13:52 asked Mar 22 '13 at 13:02 Uwe Keim 22.6k25100159 MSDN says a Bitmap loaded from a Stream requires the Stream to remain in existence as long as the Bitmap exists. –Jesse Chisholm Nov 11 '15 at 0:14 add a comment| 6 Answers 6 active oldest votes up vote 17 down vote accepted While I still did not find out the reason what exactly caused the error when saving the image, I found
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
A Generic Error Occurred In Gdi+. Asp.net C#
the workings and policies of this site About Us Learn more system.runtime.interopservices.externalexception: a generic error occurred in gdi+. about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow how to fix a generic error occurred in gdi+ 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 http://stackoverflow.com/questions/15571022/how-to-find-reason-for-generic-gdi-error-when-saving-an-image each other. Join them; it only takes a minute: Sign up A generic error occurred in GDI+ at System.Drawing.Image.Save up vote 10 down vote favorite 1 Exception: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename) Code: byte[] bitmapData = new byte[imageText.Length]; MemoryStream streamBitmap; bitmapData = http://stackoverflow.com/questions/22708150/a-generic-error-occurred-in-gdi-at-system-drawing-image-save Convert.FromBase64String(imageText); streamBitmap = new MemoryStream(bitmapData); System.Drawing.Image img = Image.FromStream(streamBitmap); img.Save(path); We convert a base64 string into a MemoryStream and then create a System.Drawing.Image (Image.FromStream(streamBitmap)). At the end the image is saved in a temp file. The strange thing is that the problem seems to occur when the activity (number of concurrent users) is high on the web server and the problem is solved temporarily after an IISRESET or an application pool recycle... ==> Garbage collector issue ? I already checked the permission of the TEMP folder... c# asp.net .net image gdi+ share|improve this question edited Mar 28 '14 at 9:46 asked Mar 28 '14 at 8:59 Razor 62021223 add a comment| 3 Answers 3 active oldest votes up vote 9 down vote When you are loading an imagefrom a Stream, You have to keep the stream open for the lifetime of the image, see MSDN Image.FromStream. I think the exception is caused because the memory stream gets closed even before the image gets disposed. You can change your code like this
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 http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed 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 Image.Save(..) throws a GDI+ exception because the memory stream is closed up vote 73 down vote favorite 29 i've got some binary data which i want to save as an image. When a generic i try to save the image, it throws an exception if the memory stream used to create the image, was closed before the save. The reason i do this is because i'm dynamically creating images and as such .. i need to use a memory stream. this is the code: [TestMethod] public void TestMethod1() { // Grab the binary data. byte[] data = File.ReadAllBytes("Chick.jpg"); // Read in the data but do not close, before using the stream. Stream originalBinaryDataStream a generic error = new MemoryStream(data); Bitmap image = new Bitmap(originalBinaryDataStream); image.Save(@"c:\test.jpg"); originalBinaryDataStream.Dispose(); // Now lets use a nice dispose, etc... Bitmap2 image2; using (Stream originalBinaryDataStream2 = new MemoryStream(data)) { image2 = new Bitmap(originalBinaryDataStream2); } image2.Save(@"C:\temp\pewpew.jpg"); // This throws the GDI+ exception. } Does anyone have any suggestions to how i could save an image with the stream closed? I cannot rely on the developers to remember to close the stream after the image is saved. In fact, the developer would have NO IDEA that the image was generated using a memory stream (because it happens in some other code, elsewhere). I'm really confused :( c# image exception gdi+ share|improve this question edited Dec 3 '08 at 8:30 Lasse V. Karlsen 222k64436634 asked Dec 3 '08 at 7:04 Pure.Krome 37.5k69264457 13 Please do not use images for code like this, makes it harder for people to copy the code in order to attempt to change it to a working state. I'll edit it to use markdown syntax. –Lasse V. Karlsen Dec 3 '08 at 8:27 add a comment| 10 Answers 10 active oldest votes up vote 120 down vote accepted As it's a MemoryStream, you really don't need to close the stream - nothing bad will happen if you don't, although obviously it's good practice to dispose anything that's disposable anyway. (See this question for more on this.) However, you should be disposing the Bitmap -