A generic error occured in GDI+
I'm attempting to resize and image using the following function and receiving this error:
Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
on this line:
imageConvertedToBitmap.Save(cachedFileName, info[1], encoderParameters);
Any ideas why this might be happening?
private byte[] GetCachedImage(string cachedFileName, string pathToImage, int width, int height)
{
if (!System.IO.File.Exists(cachedFileName) || (System.IO.File.GetLastWriteTime(pathToImage) > System.IO.File.GetLastWriteTime(cachedFileName)))
{
Image imageToResize = Image.FromFile(pathToImage);
Image imageConvertedToBitmap = new Bitmap(width, height);
Graphics graphicsController = Graphics.FromImage(imageConvertedToBitmap);
graphicsController.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsController.SmoothingMode = SmoothingMode.HighQuality;
graphicsController.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsController.CompositingQuality = CompositingQuality.HighQuality;
graphicsController.DrawImage(imageToResize, 0, 0, width, height);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
MemoryStream memoryStream = new MemoryStream();
imageConvertedToBitmap.Save(cachedFileName, info[1], encoderParameters);
imageConvertedToBitmap.Save(memoryStream, info[1], encoderParameters);
imageToResize.Dispose();
imageConvertedToBitmap.Dispose();
graphicsController.Dispose();
parameters.Dispose();
encoderParameters.Dispose();
return memoryStream.GetBuffer();
}
byte[] buffer = null;
try
{
FileStream fileStream = new FileStream(cachedFileName, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStrea开发者_运维知识库m);
long totalBytes = new FileInfo(cachedFileName).Length;
buffer = binaryReader.ReadBytes((Int32)totalBytes);
fileStream.Close();
fileStream.Dispose();
binaryReader.Close();
}
catch { }
return buffer;
}
It was a permissions error. Had to give appropriate Write permission to the directory I was saving to. Sometimes it's the simple things. :)
From what I read, when getting this GDI+ error, the first thing to check should be permissions as it's almost always an indicator of a security problem.
Try an overload of Save
that doesn't take EncoderParameters
as a parameter. It should work fine and produce the resized image that you're looking for.
I had this error one time, it was because I tried to save under a locked file by a Bitmap object. We just see your method but not how you use it.
I think there are several syntax problems in your code : you must use "using() { }" for all the disable objects in your code.
using(Image imageToResize = Image.FromFile(pathToImage))
using(Image imageConvertedToBitmap = new Bitmap(width, height))
etc.
{
}
Your got this error on another instance.
Firstly understand what this error means the other way:
When you get the error, go to task manager and navigate to Details tab.
Right click on the table headers and click select columns.
- Tick the GDI objects checkbox.
- Now find the name of your program in the table and check the no. of GDI objects.
- It would have exceeded 10000 or must be at 10000.
The error pops up whenever the no. of GDI objects by a program exceeds 10000 as 10000 is the limit.
Check how many times the System.Drawing.... runs in your code. Prevent objects like Fonts or Images from being created again and again. Instead set a reference point.
精彩评论