日期:2014-05-17  浏览次数:20567 次

Graphics DrawString参数无效
一个生成图片的ashx页面,执行到DrawString时就报错,重启iis后第一次或者第二次访问能正常生成图片,后续的访问执行到DrawString时就报错了。什么问题?

framework版本是2.0的,系统win2003

[ArgumentException: 参数无效。]
   System.Drawing.Graphics.CheckErrorStatus(Int32 status) +1048569
   System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format) +211
   System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y) +53
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +406
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76

public void ProcessRequest(HttpContext context)
{        Bitmap nbitmap = new Bitmap(1000, 1000);
        Graphics g = Graphics.FromImage(nbitmap);

        Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Italic);
        Brush brush = Brushes.Black;
        g.DrawString("测试字符串", font, brush, 10, 10);/////
        g.Dispose();
        font.Dispose();
        brush.Dispose();
        nbitmap.Save(context.Server.MapPath("~/upload/cert" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"));
        nbitmap.Dispose();
}
Graphics ?DrawString 参数无效

------解决方案--------------------
引用:
对象释放的顺序有没有问题呢


  Bitmap nbitmap = new Bitmap(1000, 1000);
        Graphics g = Graphics.FromImage(nbitmap);