Gdi Bitmap Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you a generic error occurred in gdi c# image save might have Meta Discuss the workings and policies of this a generic error occurred in gdi in c# windows application site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers how to solve a generic error occurred in gdi+ 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 a generic error occurred in gdi+. asp.net c# of 4.7 million programmers, just like you, helping each other. Join them; 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
A Generic Error Occured In Gdi+. Bitmap.save C#
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 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
> .NET Framework Class Libraries Question 0 Sign in to vote Hello All,I was just going
System.runtime.interopservices.externalexception (0x80004005): A Generic Error Occurred In Gdi+.
to play around by generating some bitmaps programatically.I started off with a generic error occurred in gdi+ windows 7 this simple example, expecting everything to go smoothly, but have run into a strange error.The following c# picturebox image save a generic error occurred in gdi+ code is by no means good, just something simple and complete I would expect to work:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace BitmapOutput{ public partial class http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method Form1 : Form { ///
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 http://weblogs.asp.net/anasghanem/solving-quot-a-generic-error-occurred-in-gdi-quot-exception error when I was using Infragistics charts control , the control was trying to create a temporary chart in ChartImages folder and my application was not given the write permission. Please if you are using XP, make http://blog.vishalon.net/bitmapsave-a-generic-error-occurred-in-gdi 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 a generic Comments Na, _one_ 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 a generic error until this time. anas - 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
Graphics oGraphic = Graphics.FromImage(oBitmap) Dim oBrush As New SolidBrush(Color.Black) Dim ofont As New Font("Arial", 8 ) oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10) oBitmap.Save("c:\\example.jpg",ImageFormat.Jpeg) oBitmap.Dispose() oGraphic.Dispose() You will get the above mentioned error:A generic error occurred in GDI+. This problem occurs because until the bitmap object is disposed, it creates a lock on the underlying image file. So you can save the newly generated file with different name but not overwrite the file because of lock. Now suppose you want to overwrite the file then create another bitmap from old bitmap. dispose the object of old bitmap, process new bitmap object and save the new bitmap object with original file name. The above chunk of code should be written in the following way. Dim oBitmap As Bitmap oBitmap = New Bitmap("c:\\example.jpg") Dim oGraphic As Graphics ' Here create a new bitmap object of the same height and width of the image. Dim bmpNew As Bitmap = New Bitmap(oBitmap.Width, oBitmap.Height) oGraphic = Graphics.FromImage(bmpNew) oGraphic.DrawImage(oBitmap, New Rectangle(0, 0, _ bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, _ oBitmap.Height, GraphicsUnit.Pixel) ' Release the lock on the image file. Of course, ' image from the image file is existing in Graphics object oBitmap.Dispose() oBitmap = bmpNew Dim oBrush As New SolidBrush(Color.Black) Dim ofont As New Font("Arial", 8 ) oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10) oGraphic.Dispose() ofont.Dispose() oBrush.Dispose() oBitmap.Save("c:\\example.jpg", ImageFormat.Jpeg) oBitmap.Dispose() Category: ASP .Net Post navigation ← ASP .Net Datagrid Itemcommand Event is not fired Bitmap.Save() Invalid Parameter Used → 45 thoughts on