System.Argumentexception: 'Invalid parameter. '

Asked

Viewed 967 times

0

I created a button with button24_Click_1 to take printscreen from Form1:

private void button24_Click_1(object sender, EventArgs e)
{
    var frm = new Form1();
    using (var bmp = new Bitmap(frm.Width, frm.Height))
    {
        frm.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
        pictureBox1.Image = bmp;
        //bmp.Save(@"C:\Users\Matheus Miranda\Desktop\TESTE\screen.png");
    }
}

Follow the problem:

inserir a descrição da imagem aqui

Follow Quickwacth error:

Message: "Invalid parameter."

Paramname: null

Stacktrace: in System.Drawing.Image.get_RawFormat() in System.Drawing.Graphics.Drawimage(Image image, Int32 x, Int32 y, Int32 width, Int32 height) in System.Drawing.Graphics.Drawimage(Image image image, Rectangle rect) in System.Windows.Forms.PictureBox.Onpaint(Painteventargs pe) in System.Windows.Forms.Control.Paintwitherrorhandling(Painteventargs and, Int16 layer) in System.Windows.Forms.Control.Wmpaint(Message& m) in System.Windows.Forms.Control.Wndproc(Message& m) in System.Windows.Forms.Control.Controlnativewindow.Onmessage(Message& m) in System.Windows.Forms.Control.Controlnativewindow.Wndproc(Message& m) in System.Windows.Forms.NativeWindow.Debuggablecallback(Intptr hwnd, Int32 msg, Intptr wparam, Intptr lparam)

Coded bmp.Save(); works perfectly, I just can’t add the photo directly to picturebox1. What I did wrong and how to fix ?

  • 1

    I would need to see more details, I don’t even know where the error is occurring. It is possible that the Bitmap is not generating an object. Anyway this code must give error, because it creates the object with using, keeps it in a property of another object and ends the execution by releasing the object, when accessing this property the object no longer exists.

  • @Maniero see the answer :)

1 answer

1


I solved the problem, thanks to the comment of @Manieiro.

var frm = new Form1();
Bitmap pic = new Bitmap(frm.Width, frm.Height);
Rectangle rect = new Rectangle();
rect = frm.ClientRectangle;
frm.DrawToBitmap(pic, rect);
pictureBox1.Image = pic;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.