您的当前位置:首页>关注 > 正文

自定义控件GDI绘制在主程序报错System怎么解决?_热门看点

来源:CSDN 时间:2023-01-10 09:47:52


【资料图】

C#自定义控件GDI绘制在主程序报错System.ArgumentException;

我的绘制图片内容大概如下:

private Bitmap backGroundImage=null;private Bitmap prospectImage=null;private Graphics graphics = null;////// 在背景上画前景///private void drawProspect(){    try    {        if(prospectImage!=null)  //注释            prospectImage.Dispose();  //注释        prospectImage = backGroundImage.Clone() as Bitmap;        using (graphics = Graphics.FromImage(prospectImage))        {            //画你所需要的内容        }        pictrueBox1.Image=prospectImage;    }catch(Exception ee)    {        }}

在线程高速(50ms以下一次)运行下就会随机报这个错 :

System.ArgumentException: 参数无效。.

在 System.Drawing.Image.get_Width()    在 System.Drawing.Image.get_Size()    在 System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)    在 System.Windows.Forms.PictureBox.get_ImageRectangle()    在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)    在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)    在 System.Windows.Forms.Control.WmPaint(Message& m)    在 System.Windows.Forms.Control.WndProc(Message& m)    在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

这种错误连错在哪在代码哪一行都不知道,连try也无法捕捉到。

经过各种尝试,发现我不写释放prospectImage那两行代码时,就不会报这个错(就是上面代码中有注释的两行代码)。

内存不再疯涨了,运行也稳定了,完美解决。

标签:

最新新闻:

新闻放送
Top