参数无效 Parameter not valid GDI+ System.Drawing System.Drawing.Graphics.GetHdc()异常

GDI+报“参数无效”异常

在使用GDI+绘图过程中,遇到一个错误,提示"参数无效"/“Parameter not valid”,如下

  •   Exception	{"参数无效。"}	System.Exception {System.ArgumentException}
      HResult	-2147024809	int
      Source = "System.Drawing"
    

    在 System.Drawing.Graphics.GetHdc()
    在 System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
    在 System.Drawing.BufferedGraphics.Render()
    在 System.Windows.Forms.Control.WmPaint(Message& m)
    在 System.Windows.Forms.Control.WndProc(Message& m)
    在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    参数无效
    代码十分简单,就是想绘制一个矩形。本身在Paint中无法捕获到异常,

private void Panel_Main_Paint(object sender, PaintEventArgs e)
{
    try
    {
        using (var g = e.Graphics)
        {
            g.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 100));
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

}

异常是在 Application.ThreadException 事件中捕获到的。

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

    // Add the event handler for handling non-UI thread exceptions to the event.
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
// Handle UI thread exceptions
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    // Log the exception or show a message box
    MessageBox.Show("An unhandled UI thread exception occurred:\n\n" + e.Exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    // Here you can log the exception to a file, event log, etc.
}

// Handle non-UI thread exceptions
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // Log the exception or show a message box
    Exception ex = (Exception)e.ExceptionObject;
    MessageBox.Show("An unhandled non-UI thread exception occurred:\n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    // Here you can log the exception to a file, event log, etc.
}

解决方法

最终发现问题所在,这个 Panel_Main 是一个自定义的用户控件,这里的的e.Graphics不能调用Dispose(即不能使用using)
修改代码如下,问题解决

private void Panel_Main_Paint(object sender, PaintEventArgs e)
{
    try
    {
        var g = e.Graphics;
        g.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 100));
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

然后,同样的Paint事件代码,如果是在Form的Paint事件中的话是正常的,一会没想明白是为什么。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图南科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值