Intermittent System.Drawing / GDI +“Generic”错误

时间:2013-07-11 18:00:50

标签: c# asp.net system.drawing

我有一个ASPX页面,它将查询字符串中的任何内容呈现为垂直文本并返回PNG。它很棒。

我只有一位遇到问题的客户。每隔几天页面停止工作并抛出可怕的GDI +“通用”错误。

Error: System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
...

我不知道为什么会出现错误,或者为什么它最终会消失。我能够将一个测试ASPX文件放入他们的安装中,这些文件运行类似的代码,但有一些变化,看看我是否可以查明问题。我发现如果我将ImageFormat从Png更改为Jpeg,则错误消失了。

我可以想象更改产品以呈现JPEG而不是PNG。但是我无法知道这是否最终会像现在一样间歇性地导致错误。

有谁知道会出现这样的问题?谢谢!代码如下。

更新:客户服务器是运行IIS 7.5的Windows Server 2008 R2,我的应用程序在.NET 4.0上运行。

    protected void Page_Load(object sender, EventArgs e)
    {
        byte[] image = GetImageBytes(this.Text);
        if (image != null)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "image/png";
            Response.OutputStream.Write(image, 0, image.Length);
        }
    }

    private byte[] GetImageBytes(string text)
    {
        var font = new Font("Tahoma", 11, FontStyle.Bold, GraphicsUnit.Pixel);

        // Create an image the size of the text we are writing
        Bitmap img = new Bitmap(1,1);
        var graphics = Graphics.FromImage(img);
        int width = (int)graphics.MeasureString(text, font).Width;
        int height = (int)graphics.MeasureString(text, font).Height;
        img = new Bitmap(img, new Size(width, height));

        // Draw the text onto the image
        graphics = Graphics.FromImage(img);
        graphics.Clear(Color.Transparent);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
        graphics.Flush();

        // Rotate the image to be vertical
        img.RotateFlip(RotateFlipType.Rotate270FlipNone);

        var stream = new System.IO.MemoryStream();
        img.Save(stream, ImageFormat.Png);
        stream.Position = 0;
        return stream.ToArray();
    }

1 个答案:

答案 0 :(得分:3)

I would say that the flush could be the issue

  

强制执行所有挂起的图形操作并立即返回,而不等待操作完成。

     

此成员已超载。有关此成员的完整信息(包括语法,用法和示例),请单击重载列表中的名称。

你可以尝试一下,看看你是否还有这个问题吗?

    private byte[] GetImageBytes(string text)
    {
        using (var font = new Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Pixel))
        using (var img = new Bitmap(1, 1))
        {
            int width;
            int height;
            using (var graphics = Graphics.FromImage(img))
            {
                width = (int)graphics.MeasureString(text, font).Width;
                height = (int)graphics.MeasureString(text, font).Height;
            }
            using (var realImg = new Bitmap(img, new Size(width, height)))
            {
                using (var graphics = Graphics.FromImage(realImg))
                {
                    graphics.Clear(Color.Transparent);
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
                }

                realImg.RotateFlip(RotateFlipType.Rotate270FlipNone);

                using (var stream = new System.IO.MemoryStream())
                {
                    realImg.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    stream.Position = 0;
                    return stream.ToArray();
                }
            }
        }
    }