0

我有以下代码组成托管在 Azure 上的 ASHX 文件:

public void ProcessRequest(HttpContext context)
        {
            int number;
            try
            {
                number = int.Parse(context.Request.QueryString["number"]);
            }
            catch
            {
                number = -1;
            }

            var bitmap = new Bitmap(173, 173);
            DrawImage(bitmap, number);
            context.Response.ContentType = "image/png";
            bitmap.Save(context.Response.OutputStream, ImageFormat.Png);
            context.Response.Flush();
        }

        private void DrawImage(Bitmap bitmap, int number)
        {
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.FillRectangle(GetBrush(number), 0, 0, 173, 173);

                Font font = new Font("Segoe UI", 40f, FontStyle.Bold);
                StringFormat textFormat = new StringFormat
                                              {
                                                  Alignment = StringAlignment.Far,
                                                  LineAlignment = StringAlignment.Center
                                              };
                Rectangle rectangle = new Rectangle(0, 0, 160, 173);
                graphics.DrawString("76", font, Brushes.Black, rectangle, textFormat);

                font = new Font("Arial", 16, FontStyle.Bold);
                textFormat = new StringFormat
                                 {
                                     Alignment = StringAlignment.Near,
                                     LineAlignment = StringAlignment.Near
                                 };
                rectangle = new Rectangle(10, 10, 163, 163);
                graphics.DrawString("YOUR NUMBER", font, Brushes.Black, rectangle, textFormat);
            }
        }

在本地运行时,此代码运行良好并生成正确的结果。部署到 Azure 时,我得到以下信息:

GDI+ 中出现一般错误。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Runtime.InteropServices.ExternalException:GDI+ 中发生一般错误。

有人有想法么?提前致谢

4

1 回答 1

0

所以,我找到了这个问题的答案,或者至少找到了这个问题的答案之一。

感谢Rick Strahls 的博客条目。虽然位图克隆技术是朝着正确方向迈出的一步(为什么是微软?)显然,当您尝试从 ashx 输出 PNG 时,您必须特别考虑。

这种考虑基本上是在将位图保存到实际位置之前将其保存到 MemoryStream 中。

于 2012-07-07T13:44:14.723 回答