C# 间歇系统。图纸/GDI+&引用;“通用”;错误

C# 间歇系统。图纸/GDI+&引用;“通用”;错误,c#,asp.net,system.drawing,C#,Asp.net,System.drawing,我有一个ASPX页面,它将查询字符串中的任何内容呈现为垂直文本,并返回PNG。它工作得很好 我正好有一位客户遇到了问题。每隔几天,页面就会停止工作并抛出可怕的GDI+“通用”错误 我不知道为什么会出现错误,或者为什么它最终会消失。我能够将一个测试ASPX文件扔进他们的安装中,该文件运行了类似的代码,但有一些变化,看看我是否能够找出问题所在。我发现,如果我将ImageFormat从Png更改为Jpeg,错误就会消失 可以想象,我可以将产品改为渲染JPEG而不是PNG。然而,我无法知道这是否最终会像

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

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

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

可以想象,我可以将产品改为渲染JPEG而不是PNG。然而,我无法知道这是否最终会像现在这样间歇性地导致错误

有人知道什么会导致这样的问题吗?谢谢代码如下

更新:客户服务器是运行IIS 7.5的Windows server 2008 R2 box,我的应用程序运行在.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();
    }

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

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

你能试试看你是否还有这个问题吗

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

这可能是一个有缺陷的图形卡驱动程序。还要检查桌面堆是否已用完。如果已满,则会在系统事件日志中写入事件日志条目。检查应用程序和系统事件日志中是否存在可疑事件。您可能应该处理字体、img、图形和流对象。@LarsTech,是的,这就是我在回答中建议使用新代码的原因:)Alois,我将检查客户服务器中的系统日志,谢谢。我做了一些进一步的测试——我将这段代码复制到一个控制台应用程序中,该应用程序使用随机文本字符串生成许多图像。我在300万张没有问题的图片上停止了它。Fredou-我使用一个控制台应用程序在一个进程中运行了数百万次代码,我没有遇到任何问题。然而,无论如何,这似乎是对代码的一个很好的修改。谢谢抱歉,刚才注意到您还删除了对Flush的调用以及using语句。我会尝试一下。@BarryFandango,是的,我删除了它,因为我看不到刷新调用的必要性,而且我还更新了代码,因为我忘记了在图形上使用。您没有处理图形对象。就我个人而言,我会在打开流对象或RotateFlip调用之前关闭该图形对象。aaaaaaaaaaaaaaaaa自从我写了这条评论后,你做了一些编辑。@LarsTech,我注意到了,并且在6分钟前修复了它:-)
    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();
                }
            }
        }
    }