Interopservices Externalexception A Generic Error Occurred In
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 a generic error occurred in gdi c# image save About Us Learn more about Stack Overflow the company Business Learn more about a generic error occurred in gdi bitmap save c# hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss system.runtime.interopservices.externalexception (0x80004005): a generic error occurred in gdi+. 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 A generic error how to solve a generic error occurred in gdi+ 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 = 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 Generic Error Occurred In Gdi In C# Windows Application
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 byte[] bitmapData = new byte[imageText.Length]; bitmapData = Convert.FromBase64String(imageText); using (var streamBitmap = new MemoryStream(bitmapData) { using (img = Image.FromStream(streamBitmap)) { img.Save(path); } } Here are some links to threads discussing similar problems gdi+ error saving image from webpage When drawing an image: System.Runtime.Intero
here for a quick overview of the site Help Center Detailed answers to any questions you might have
How To Fix A Generic Error Occurred In Gdi+
Meta Discuss the workings and policies of this site About Us Learn a generic error occurred in gdi+. asp.net c# more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us a generic error occurred in gdi+. at system.drawing.image.save memorystream 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 http://stackoverflow.com/questions/22708150/a-generic-error-occurred-in-gdi-at-system-drawing-image-save you, helping each other. Join them; it only takes a minute: Sign up A generic error occurred in GDI+, JPEG Image to MemoryStream up vote 203 down vote favorite 38 This seems to be a bit of an infamous error all over the web. So much so that I have been unable to find an answer http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream to my problem as my scenario doesn't fit. An exception gets thrown when I save the image to the stream. Weirdly this works perfectly with a png but gives the above error with jpg and gif which is rather confusing. Most similar problem out there relate to saving images to files without permissions. Ironically the solution is to use a memory stream as I am doing.... public static byte[] ConvertImageToByteArray(Image imageToConvert) { using (var ms = new MemoryStream()) { ImageFormat format; switch (imageToConvert.MimeType()) { case "image/png": format = ImageFormat.Png; break; case "image/gif": format = ImageFormat.Gif; break; default: format = ImageFormat.Jpeg; break; } imageToConvert.Save(ms, format); return ms.ToArray(); } } More detail to the exception. The reason this causes so many issues is the lack of explanation :( System.Runtime.InteropServices.ExternalException was unhandled by user code Message="A generic error occurred in GDI+." Source="System.Drawing" ErrorCode=-2147467259 StackTrace: at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format) at Caldoo.Infrastructure.PhotoEditor.ConvertImageToByteArray(Image imageToConvert) in C:\Users\Ian\SVN\Caldoo\Caldoo.Coordinator\PhotoEditor.cs:line 139 at Caldoo.Web.Controllers.PictureController.Croppable() in C:\Users\Ian\SVN\Caldoo\Caldoo.Web\Controllers\PictureController.cs:line 132 at lambda_method(ExecutionScope , Con
ASP.NET Community Standup Forums Help Home/ASP.NET Forums/Advanced ASP.NET/System.Drawing/GDI+/System.Runtime.InteropServices.ExternalException: A generic error occ... System.Runtime.InteropServices.ExternalException: A generic error occurred http://forums.asp.net/t/1353341.aspx?System+Runtime+InteropServices+ExternalException+A+generic+error+occurred+in+GDI+ in GDI+. [Answered]RSS 9 replies Last post Dec 23, 2013 https://social.msdn.microsoft.com/Forums/vstudio/en-US/437dafaf-ebad-4f38-b1d5-ab9d15949aa8/systemruntimeinteropservicesexternalexception-a-generic-error-occurred-in-gdi?forum=csharpgeneral 04:29 AM by Vkas ‹ Previous Thread|Next Thread › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Reply kapilbharat Member 50 Points 152 Posts System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. Nov 26, a generic 2008 07:36 AM|kapilbharat|LINK First of all good evenning all. I am facing anerror --System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. its very strange that locallymy code working fine my code is ///
error occurred in GDI+. Visual Studio Languages , .NET Framework > Visual C# Question 0 Sign in to vote I'm converting a byte array to an image and then save it to disk: MemoryStream ms = new MemoryStream(mybytearray); Bitmap x = new Bitmap(Image.FromStream(ms, true, true), 100, 100); x.Save(@"c:\image.png"); When the method is called multiple times I get the following exception: System.Runtime.InteropServices.ExternalException: 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) Edited by FAndrei Friday, February 03, 2012 10:19 PM Friday, February 03, 2012 10:19 PM Reply | Quote Answers 1 Sign in to vote Hi sir, I have just modified your code a bit by including it in functions... I clicked on the button 101 times and no error.... working perfect Please try this one : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public byte[] imageToByteArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return ms.ToArray(); } public Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Bitmap returnImage = new Bitmap(Image.FromStream(ms,true,true),100,100); returnImage.Save(@"C:\Data\Rehan1.png"); return returnImage; } private void button1_Click(object sender, EventArgs e) { try { byte[] byt = imageToByteArray(Image.FromFile(@"C:\Data\Rehan.jpg")); pictureBox1.Image = byteArrayToImage(byt); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } As you have included memory stream objects in the function, they have local scope so disposing will not be an issue.. with your code after you complete your processing you need to dispose all memory objects and bytes so this is a good alternative to use it in functions. Please mark as answer if this post helps you and unpropose it as answer if it does not h