A Generic Error Occurred In
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might asp.net a generic error occurred in gdi+ have Meta Discuss the workings and policies of this site About a generic error occurred in gdi+ crystal reports Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads a generic error occurred in gdi+ windows 7 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
A Generic Error Occurred In Gdi+ Image.save C#
programmers, just like 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 198 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 a generic error occurred in gdi+ vb to find an answer 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 a
have a write permission on some directory. For example, if you are trying to save the Image from the memory stream to the file system , you may get that error. I also faced that error when a generic error occurred in gdi+ at system drawing image save I was using Infragistics charts control , the control was trying to create a temporary
A Generic Error Occurred In Gdi+ Selenium
chart in ChartImages folder and my application was not given the write permission. Please if you are using XP, make sure to add
Generic Error Occurred In Gdi+ Bitmap Save
write permission for the aspnet account on that folder. If you are using windows server (2003,2008) or Vista, make sure that add write permission for the Network service account. Hope it help some one. 23 Comments Na, _one_ http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream of the possible errors could be the one, you described. The "Generic error" in fact is used in certain scenarios. It means "we were to lazy to write better error return code". This really sucks. Uwe - Saturday, February 28, 2009 8:44:55 PM Well, at least that's based on my experience and on the problems i faced. I know that there is many other causes of that problem,but that's what i know until this time. anas - http://weblogs.asp.net/anasghanem/solving-quot-a-generic-error-occurred-in-gdi-quot-exception Saturday, February 28, 2009 8:57:28 PM I think it's the most common error so permissions should always be checked first. SGWellens - Saturday, February 28, 2009 9:18:12 PM It might not be that you don't have write permissions, but a sharing violation might be in effect. Common scenario that this happens: 1) Read an image into a Bitmap object 2) Manipulate the bitmap 3) Wanting to write back the bitmap to overwrite the original image file you read in step 1. This can happen because the Bitmap implementation seems to be as lazy as possible. It can choose the just remember the path that was the source of the image bits (or even a handle to the opened file they came from). This then means your software has the original image file open, causing this problem when you try to overwrite it in step three. To work around this use some simple code in an intermediate step (let's say 1a) that creates a new, blank Bitmap object with the same width/height/colordepth as the image you read. Then, get a Graphics object for the blank image and draw (BitBlt, basically) the loaded image onto the blank image. Then, dispose the loaded image. This gets you a Bitmap object that contains the content of the loaded image actually stored in memory, without any link to the original image file. Th
to: public void SaveImage() { byte[] byteArray = null; // Put the https://www.roelvanlisdonk.nl/2011/12/30/solving-a-generic-error-occurred-in-gdi-in-c/ bytes of the image here.... Image result = null; ImageFormat http://www.hanselman.com/blog/TheWeeklySourceCode50ALittleOnAGenericErrorOccurredInGDIAndTroubleGeneratingImagesOnWithASPNET.aspx format = ImageFormat.Png; using (MemoryStream ms = new MemoryStream(byteArray)) { result = Image.FromStream(ms); } using (Image imageToExport = result) { string filePath = string.Format(@"C:\Temp\Myfile.{0}", format.ToString()); imageToExport.Save(filePath, format); } } This resulted in the error: "A generic error occurred in GDI+." generic error The error was resolved, after changing the code to: public void SaveImage() { byte[] byteArray = null; // Put the bytes of the image here.... Image result = null; ImageFormat format = ImageFormat.Png; result = new Bitmap(new MemoryStream(byteArray)); using (Image imageToExport = result) { string filePath = string.Format(@"C:\Temp\Myfile.{0}", generic error occurred format.ToString()); imageToExport.Save(filePath, format); } }
I found the solution at: http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream Categorized in Uncategorized Tagged with C# View all 4 comments Leave a Reply Cancel reply Your email address will not be published. Required fields are marked *Comment Name * Email * Website 4 comments Dino says: January 13, 2012 at 17:57 Hello I find on web that you have similar error like I get now and I don't know how to resole. In my C#.NET application I catch webcam picture in pictureBox and try to save on disk with SaveFileDialog, but I get always the same error "A generic error occurred in GDI+". Code is blow, if you can help… ======================= SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = @"slike"; sfd.Filter = "*.jpg|*.jpg"; sfd.Title = "Spremi sliku kao…"; sfd.FilterIndex = 1; if (sfd.ShowDialog() == DialogResult.OK) { try { using (Bitmap bitmap = new Bitmap(pictureBox1.Image)) { bitmap.Save(sfd.FileASP.NET|Source Code Sponsored By I got a nice little Yellow Screen of Death (YSOD) error on some code running under IIS that worked fine when running on the VS Developer Web Server. The error was "A generic error occurred in GDI+" and you know that if an error is generic, it's sure in the heck not specific. My little application takes an overhead map that's stored in a local file, does some calculations from user input and draws an X on the map, then returns the resulting dynamically generated image. There's basically three ways to do images on the server side. Use Native APIs and Interop, which only works in full trust, use System.Drawing, which "isn't supported" or use WPF on the server side, which also, ahem, isn't officially supported. I'm still trying to figure out why, but just to be clear, I used System.Drawing in extremely high traffic sites with no problems. As long as paid close attention to my unmanaged resources, I have never had a problem. I've heard anecdotally of people having trouble with GDI+ (System.Drawing) and switching over to WPF and having no problem with that. As with all things, test what you're doing. There's even some ASP.NET Controls on CodePlex that might help. Now this post can't answer ALL reasons you're getting "a generic error occurred in GDI+" but it can answer mine. In my particular case (and I think this is the most common mistake) I was saving the composited image as a PNG. First, I'll show you a little chunk of a code from 5 years ago that took two images and built a single image from them. public class SomeCheckImageHandler : IHttpHandler{ //some stuff snipped public SomeCheckImageHandler(){} public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpg"; //some stuff snipped GetCheckImageRequest req = new GetCheckImageRequest(); //some stuff snipped, get the params from the QueryString GetCheckImageResponse res = banking.GetCheckImage(req); //some stuff snipped if (res.ImageBack != null) { //merge them into one image using(MemoryStream m = new MemoryStream(res.BackImageBytes)) using(Image backImage = System.Drawing.Image.FromStream(m)) using(MemoryStream m2 = new MemoryStream(res.BrontImageBytes)) using(Image frontImage = System.Drawing.Image.FromStream(m2)) using(Bitmap compositeImage = new Bitmap(frontImage.Width,frontImage.Height+backImage.Height)) using(Graphics compositeGraphics = Graphics.FromImage(compositeImage)) { compositeGraphics.CompositingMode = CompositingMode.SourceCopy; compositeGraphics.DrawImageUnscaled(frontImage,0,0); compositeGraphics.DrawImageUnscaled(backImage,0,frontImage.Height); compositeImage.Save(