gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:10:39 28 4
gpt4 key购买 nike

我有一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com