Graphics DrawString参数无效

Go 旅城通票 2013-08-28 11:46:36
加精
一个生成图片的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();
}
...全文
3323 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nbh_king 2015-10-26
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分
EnForGrass 2013-08-28
  • 打赏
  • 举报
回复
引用 1 楼 Chinajiyong 的回复:
对象释放的顺序有没有问题呢
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);///// font.Dispose(); brush.Dispose(); g.Dispose(); //font.Dispose(),brush.Dispose()删除试试 nbitmap.Save(context.Server.MapPath("~/upload/cert" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg")); nbitmap.Dispose();
EnForGrass 2013-08-28
  • 打赏
  • 举报
回复
对象释放的顺序有没有问题呢
jshi123 2013-08-28
  • 打赏
  • 举报
回复
Brushes.Black是静态对象,Dispose释放对应的HBrush句柄,以后再调用就出错了。 从报错信息看也很明显,这是DrawString方法的最后两行: int status = SafeNativeMethods.Gdip.GdipDrawString(new HandleRef(this, this.NativeGraphics), s, s.Length, new HandleRef(font, font.NativeFont), ref layoutRect, new HandleRef(format, handle), new HandleRef(brush, brush.NativeBrush)); this.CheckErrorStatus(status); 前一句调用native gdi函数去执行,后一句检查win32返回状态码。 在CheckErrorStatus方法中,如果status != 0,则: throw SafeNativeMethods.Gdip.StatusException(status); 把error status转成相应的各类Excetion 其中: case 2: return new ArgumentException(SR.GetString("GdiplusInvalidParameter")); 你看到的就是ArgumentException,对应win32 error code是2 表示传到GdipDrawString函数中的某个参数是无效的。
Go 旅城通票 2013-08-28
  • 打赏
  • 举报
回复
引用 7 楼 guwei4037 的回复:
测试过有效的代码:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    string str = Server.MapPath("~/upload/test.jpg");
                    System.Drawing.Image myImage = System.Drawing.Image.FromFile(str);
                    Bitmap map = new Bitmap(myImage);
                    myImage.Dispose();
                    Graphics graphics = Graphics.FromImage(map);
                    graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
                    SolidBrush brush = new SolidBrush(Color.Red);
                    PointF P = new PointF(100, 100);
                    Font font = new Font("宋体", 40);
                    graphics.DrawString("guwei4037", font, brush, P);
                    map.Save(str.Substring(0, str.LastIndexOf("\\") + 1) + "new" + str.Substring(str.LastIndexOf("\\") + 1, str.LastIndexOf(".") - str.LastIndexOf("\\") - 1) + str.Substring(str.LastIndexOf("."), str.Length - str.LastIndexOf(".")), ImageFormat.Jpeg);
                    font.Dispose();
                    graphics.Dispose();
                }
                catch { }
            }
        }
看来真是画笔的问题。。
tan598121925 2013-08-28
  • 打赏
  • 举报
回复
学习了
Go 旅城通票 2013-08-28
  • 打赏
  • 举报
回复
nnd。。。。
// Brush brush = Brushes.Black;
SolidBrush brush = new SolidBrush(Color.Black);
改成Brush的子类就没问题了,搞什么飞机。。。ms的bug吗?谁能给解释下什么问题噶。。。 等等看有木有人有其他的观点,晚点结贴。。
全栈极简 2013-08-28
  • 打赏
  • 举报
回复
测试过有效的代码:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    string str = Server.MapPath("~/upload/test.jpg");
                    System.Drawing.Image myImage = System.Drawing.Image.FromFile(str);
                    Bitmap map = new Bitmap(myImage);
                    myImage.Dispose();
                    Graphics graphics = Graphics.FromImage(map);
                    graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
                    SolidBrush brush = new SolidBrush(Color.Red);
                    PointF P = new PointF(100, 100);
                    Font font = new Font("宋体", 40);
                    graphics.DrawString("guwei4037", font, brush, P);
                    map.Save(str.Substring(0, str.LastIndexOf("\\") + 1) + "new" + str.Substring(str.LastIndexOf("\\") + 1, str.LastIndexOf(".") - str.LastIndexOf("\\") - 1) + str.Substring(str.LastIndexOf("."), str.Length - str.LastIndexOf(".")), ImageFormat.Jpeg);
                    font.Dispose();
                    graphics.Dispose();
                }
                catch { }
            }
        }
Go 旅城通票 2013-08-28
  • 打赏
  • 举报
回复
引用 3 楼 jshi123 的回复:
静态刷子Brushes.Black不需要Dispose吧。
应该不是这个问题。。刚才测试注释掉 font.Dispose();也只能重启iis才能生成图片,并且是第一次,第二次访问没报错,后续的访问还是到DrawString那不就参数错误了 大家新建ashx页面运行测试下。我这里就是这个问题,不知道你们那边出这种问题吗 或者哪个能提供一个向图片中写入文字的demo,我这边测试没问题就结贴了
Go 旅城通票 2013-08-28
  • 打赏
  • 举报
回复
引用 2 楼 Chinajiyong 的回复:
[quote=引用 1 楼 Chinajiyong 的回复:] 对象释放的顺序有没有问题呢
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);///// font.Dispose(); brush.Dispose(); g.Dispose(); //font.Dispose(),brush.Dispose()删除试试 nbitmap.Save(context.Server.MapPath("~/upload/cert" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg")); nbitmap.Dispose();[/quote] 注释掉了还是不行
jshi123 2013-08-28
  • 打赏
  • 举报
回复
静态刷子Brushes.Black不需要Dispose吧。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧