Skip to main content
 首页 » 编程设计

C# winforms 绘制到位图无效参数

2024年02月20日17bhlsheji

我已经做了一个应用程序,我需要函数 drawbitmap 来打印我的面板。当我按下按钮 (btnUpdate) 12 次或更多次时,我根据此规则收到参数异常(无效参数):panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));

private void preview() 
        { 
            Bitmap bmp1 = new Bitmap(2480, 3508); 
            panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508)); 
            pictureBox2.Image = bmp1; 
        } 
 
        private void btnUpdate_Click(object sender, EventArgs e) 
        { 
            preview(); 
        } 

有人可以帮帮我吗?

我不能使用 bmp1.Dispose(); 函数...我在 Program.cs 文件中的这一行中得到一个异常(exception):Application.Run(new Form1( ));

请您参考如下方法:

这可能是您在处理完位图后没有处理它们的情况。试试这个:

panel1.DrawToBitmap(...); 
 
// get old image  
Bitmap oldBitmap = pictureBox2.Image as Bitmap; 
 
// set the new image 
pictureBox2.Image = bmp1; 
 
// now dispose the old image 
if (oldBitmap != null) 
{ 
    oldBitmap.Dispose(); 
}