Asp Net Bitmap Save A Generic Error Occurred In Gdi
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings a generic error occurred in gdi c# image save and policies of this site About Us Learn more about Stack a generic error occurred in gdi+. bitmap.save c# Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs a generic error occurred in gdi in c# windows application 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;
How To Solve A Generic Error Occurred In Gdi+
it only takes a minute: Sign up A Generic error occured in GDI+ in Bitmap.Save method up vote 26 down vote favorite 8 I am working on to upload and same a thumnail copy of that image in a thumbnail forder. I am using following link: http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx but newBMP.Save(directory + "tn_" + filename); is causing exception "A generic error occurred a generic error occurred in gdi+. asp.net c# in GDI+." I have tried to give permission on folder, also tried to use a new separate bmp object when saving. Edit: protected void ResizeAndSave(PropBannerImage objPropBannerImage) { // Create a bitmap of the content of the fileUpload control in memory Bitmap originalBMP = new Bitmap(fuImage.FileContent); // Calculate the new image dimensions int origWidth = originalBMP.Width; int origHeight = originalBMP.Height; int sngRatio = origWidth / origHeight; int thumbWidth = 100; int thumbHeight = thumbWidth / sngRatio; int bannerWidth = 100; int bannerHeight = bannerWidth / sngRatio; // Create a new bitmap which will hold the previous resized bitmap Bitmap thumbBMP = new Bitmap(originalBMP, thumbWidth, thumbHeight); Bitmap bannerBMP = new Bitmap(originalBMP, bannerWidth, bannerHeight); // Create a graphic based on the new bitmap Graphics oGraphics = Graphics.FromImage(thumbBMP); // Set the properties for the new graphic file oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new graphic based on the resized bitmap oGraphics.DrawImage(originalBMP, 0, 0, thumbWidth, thumbHeight); Bitmap newBitmap = new Bitmap(thumbBMP); thumbBMP.Dispose(); thumbBMP = null; // Save the new graphic file to the server newBitmap.Save("~/image/thumbs/" + "t" + ob
here for a quick overview of the site Help Center Detailed answers to any questions you might c# picturebox image save a generic error occurred in gdi+ have Meta Discuss the workings and policies of this site About
System.runtime.interopservices.externalexception (0x80004005): A Generic Error Occurred In Gdi+.
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 http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method programmers, just like you, helping each other. Join them; it only takes a minute: Sign up A Generic error occurs at GDI+ at Bitmap.Save() after using SaveFileDialog up vote 20 down vote favorite 3 I use the following code block with some more code inside the using block: using (System.Drawing.Bitmap tempImg = (System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap)) { // ... http://stackoverflow.com/questions/5813633/a-generic-error-occurs-at-gdi-at-bitmap-save-after-using-savefiledialog tempImg.Save("..\\..\\testdata\\tempImg.bmp", ImageFormat.Bmp); // ... } But I still get the error: A Generic Error occured at GDI+ only after I make some action which is not related to the code inside the using block. In other times this works well. Also the tempImg.bmp is a temporary file, so I delete the tempImg.bmp within the using block itself. Since the tempImg is inside the using and this it's disposed, I think the locking problem should be solved. Can someone please let me know what is the mistake in this code? Edit: System.Drawing.Image to System.Drawing.Bitmap as the type of tempImg. Edit: I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'. c# bitmap save savefiledialog share|improve this question edited May 5 '11 at 9:32 asked Apr 28 '11 at 4:28 Dulini Atapattu 1,73642442 Why is tempImg Image and your cast Bitmap ? –V4Vendetta Apr 28 '11 at 4:33 Sorry, it should be corrected as SYstem.Drawin
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 http://weblogs.asp.net/anasghanem/solving-quot-a-generic-error-occurred-in-gdi-quot-exception I was using Infragistics charts control , the control was trying to create a temporary chart https://www.roelvanlisdonk.nl/2011/12/30/solving-a-generic-error-occurred-in-gdi-in-c/ in ChartImages folder and my application was not given the write permission. Please if you are using XP, make sure to add 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_ of a generic 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 - Saturday, February a generic error 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. Then, work on the
to: public void SaveImage() { byte[] byteArray = null; // Put the bytes of the image here.... Image result = null; ImageFormat 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+." 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}", 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.FileName, ImageFormat.Jpeg); sfd.Dispose();//To Do…. isprobaj bitmap.Dispose();//To Do…. isprobaj } MessageBox.Show("Slika je spremljena! ", "Spremanje slike", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Greška pri spremanju slike: \n" + ex.Message); } finally { sfd.Dispose();// to check….. } } sfd.Dispose();// to check….. button6.Enabled = true; button7.Enable