ASP.NET .ASHX图像生成。 NotSupportedException异常

时间:2013-07-12 08:45:42

标签: asp.net vb.net ashx notsupportedexception

我正在使用以下代码在我的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个这些图像时跳过它们可能非常耗费时间。如果由于这个原因图像有可能会失败,我相信我的客户会不会留下深刻的印象。

1 个答案:

答案 0 :(得分:1)

一些建议:

不应该调用显式dispose,而应该在using块中包含img和graphics的使用。

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

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
相关问题