开发者

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:

  1. When you get the error, go to task manager and navigate to Details tab.

  2. Right click on the table headers and click select columns.

  3. Tick the GDI objects checkbox.
  4. Now find the name of your program in the table and check the no. of GDI objects.
  5. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜