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 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...
2 Reasons Why This Generic GDI Error Occurred Due to the lock when you try to save and overwrite your modified bitmap, it throws this error. When you are saving a file, make sure the user has Write permission on the folder.
Bitmap.Save (): A generic error occurred in GDI+ Why This Generic GDI Error Occurred? When you initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to the lock when you try to save and overwrite your modified bitmap, it throws this error.
As the exception does not provide more details, it is very frustrating to figure out the root cause. GDI+ throws an error when it cannot save file. Following are 2 reasons why this error occurs. When you are initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file.
As gaffleck said it would be nice if GDI+ had thrown more informative exception. I had the same generic exception, but then I gave write permission to the IIS on parallel plesk file manager. if you are on windows server, make sure to give write permission to IIS user which was IIS_IUSR in my server
Due to the lock when you try to save and overwrite your modified bitmap, it throws this error. When you are saving a file, make sure the user has Write permission on the folder. This is important when you are getting this error on the Website because Website runs under restricted permissions.
When you are loading an imagefrom a Stream, You have to keep the stream open for the lifetime of the image, see this 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.InteropServices.ExternalException: A generic error occurred in GDI
Make sure that the path you specified is valid. Using the previous answer (with the usings on the memory stream) you may still get this exact error "Generic Error in GDI+" if the file path does not exist. The file will be created, the directory path must exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With