c# - 间歇性 System.Drawing/GDI+ "Generic"错误

标签 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();
    }

最佳答案

I would say that the flush could be the issue

Forces execution of all pending graphics operations and returns immediately without waiting for the operations to finish.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

你能试试看这个问题是否仍然存在吗?

    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();
                }
            }
        }
    }

关于c# - 间歇性 System.Drawing/GDI+ "Generic"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17600293/

相关文章:

c# - ASP.NET 集成测试 - TestDrive.net 不会运行它们,其他测试运行者会

c# - 从 gridview 插入到我的数据库中的值从第二列开始对于其余列都是相同的

c# - Image.Save 不将图像数据保存到文件

c# - 在 WinForms 中绘制垂直堆叠的文本

c# - 无限 Do...while 循环使用 Async Await

c# - POST 请求的 HttpWebRequest 的简单替代方案?

c# - 根据 AD 验证凭据

c# - 计算缩放级别以使图像适合面板

c# - Visual C# - 将文本框的内容写入 .txt 文件

c# - 为什么这个 string.Format() 不返回字符串,而是动态的?