可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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...
回答1:
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.InteropServices.ExternalException: A generic error occurred in GDI
回答2:
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.
回答3:
I encountered the same exception message when saving an image. It turned out my code was fine and doing what it was supposed to do.
The problem was that the hard drive was full, thus the new image couldn't be created. I only noticed this when trying to save the project I was working on, as it didn't have space to save.
回答4:
In my case, Below snippet works fine where ConvertedImageString is Base64Image string received from API and I convert that into related image with format and will save it to physical file folder on Server. Edit: The above error occurs might be because of wrong file path on which you are trying to Save image
string converted = ConvertedImageString.Replace('-', '+'); converted = converted.Replace('_', '/'); using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(ConvertedImageString))) { using (Bitmap bm1 = new Bitmap(ms)) { newFileName = id + ".jpg"; newFileName = CleanFileName(newFileName); newFileName = newFileName.Replace(" ", "_"); Path = Path + newFileName; bm1.Save(Path, ImageFormat.Jpeg); } }