0

我正在使用以下代码在我的 ASP.NET Web 应用程序中动态绘制图像。

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim js As New JavaScriptSerializer
    Dim ai As absencestruct = js.Deserialize(Of absencestruct)(CStr(context.Request.QueryString.Item("json")))

    'Ensure font is readable (half height of block or 12px whichever is smaller)
    Dim f As Font = New Font("Calibri", Math.Min(CInt(CDbl(ai.size.height) / 2.5), 12), GraphicsUnit.Pixel)

    Dim img As New Bitmap(CInt(ai.size.width), CInt(ai.size.height))
    Dim g As Graphics = Graphics.FromImage(img)

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality

    If ai.textStyle <> textStyleEnum.invisible Then
        g.Clear(Color.White)
    End If

    Dim r As New Rectangle(1, 1, CInt(ai.size.width) - 1, CInt(ai.size.height) - 1)

    r = New Rectangle(r.Left + 2, r.Top + 2, r.Width - 4, r.Height - 4)

    drawAbsence(ai, True, r, g)
    If ai.abs2.type <> AbsenceItem.halfDays.Errored AndAlso ai.drawStyle = AbsenceItem.drawStyle.orphan Then
        drawAbsence(ai, False, r, g)
    End If

    Dim s As SizeF = g.MeasureString(ai.day.ToString, f)
    Dim br As SolidBrush = CType(Brushes.Black, SolidBrush)
    If ai.textStyle = textStyleEnum.otherMonth Then br = CType(Brushes.Gray, SolidBrush)

    If ai.textStyle <> textStyleEnum.invisible Then
        g.DrawString(ai.day.ToString, f, br, New Point(CInt((CDbl(ai.size.width) / 2) - CInt(s.Width / 2)), CInt((CDbl(ai.size.height) / 2) - CInt(s.Height / 2))))
    End If

    context.Response.ContentType = "image/png"
    img.Save(context.Response.OutputStream, ImageFormat.Png)
    img.Dispose()
End Sub

以前这工作得很好,但是突然之间,一旦它到达

img.dispose

我得到一个异常 NotSupportedException “不支持指定的方法”

我删除了 dispose 调用以查看会发生什么...此时我在

End Sub

因为我认为这几乎是不可能的,所以我尝试重新启动 PC 和 Visual Studio 2012。没有效果。

应该注意的是,如果我跳过错误,图像工作得非常好,但是我相信当页面绘制任何多达 50 个跳过它们的图像时,你会欣赏到这可能会非常耗时,除此之外,如果因此,图像可能会失败,我相信我的客户不会留下深刻的印象。

4

1 回答 1

1

几个建议:

您应该将 img 和 graphics 的使用包装在 Using 块中,而不是调用显式 dispose。

您应该刷新并关闭响应流,以确保在处理图像之前使用完毕。

Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements IHttpHandler.ProcessRequest
    Dim js As New JavaScriptSerializer
    Dim ai As absencestruct = js.Deserialize(Of absencestruct)(CStr(context.Request.QueryString.Item("json")))

    'Ensure font is readable (half height of block or 12px whichever is smaller)
    Dim f As Font = New Font("Calibri", Math.Min(CInt(CDbl(ai.size.height) / 2.5), 12), GraphicsUnit.Pixel)

    Using img As New Bitmap(CInt(ai.size.width), CInt(ai.size.height))
        Using g As Graphics = Graphics.FromImage(img)

            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality

            If ai.textStyle <> textStyleEnum.invisible Then
                g.Clear(Color.White)
            End If

            Dim r As New Rectangle(1, 1, CInt(ai.size.width) - 1, CInt(ai.size.height) - 1)

            r = New Rectangle(r.Left + 2, r.Top + 2, r.Width - 4, r.Height - 4)

            drawAbsence(ai, True, r, g)
            If ai.abs2.type <> AbsenceItem.halfDays.Errored AndAlso ai.drawStyle = AbsenceItem.drawStyle.orphan Then
                drawAbsence(ai, False, r, g)
            End If

            Dim s As SizeF = g.MeasureString(ai.day.ToString, f)
            Dim br As SolidBrush = CType(Brushes.Black, SolidBrush)
            If ai.textStyle = textStyleEnum.otherMonth Then br = CType(Brushes.Gray, SolidBrush)

            If ai.textStyle <> textStyleEnum.invisible Then
                g.DrawString(ai.day.ToString, f, br, New Point(CInt((CDbl(ai.size.width) / 2) - CInt(s.Width / 2)), CInt((CDbl(ai.size.height) / 2) - CInt(s.Height / 2))))
            End If

            context.Response.ContentType = "image/png"
            img.Save(context.Response.OutputStream, ImageFormat.Png)
            context.Response.Flush()
            context.Response.Close()
        End Using
    End Using
End Sub
于 2013-07-12T19:23:56.650 回答