SuperPrograman
SuperPrograman

Reputation: 1822

C++ panel DrawToBitmap not working right

I am trying to save a panel (panel1) as an image using DrawToBitmap, and that I have been able to do. The problem is, that panel1 is inside another panel with panel1 Location not equal to 0, 0. So when the image is captured, it for some reason does not capture at the top left of panel1 but at Location(0, 0) of it's parent. Here is the code I have.

Bitmap^ bmp = gcnew Bitmap(panel1->Width, panel1->Height);
panel1->DrawToBitmap(bmp, panel1->Bounds);
bmp->Save("Capture.bmp");
delete bmp;

It is capturing with the width and height of panel1, but that is cutting off the bottom right corner of the panel. Thanks in advance...

Upvotes: 0

Views: 756

Answers (1)

ildjarn
ildjarn

Reputation: 62985

Use panel1->ClientRectangle instead of panel1->Bounds, and panel1->ClientSize rather than panel1->Width and panel1->Height.

Additionally, don't use gcnew unless you have to – your code, as-is, is not exception-safe. Use RAII instead, just like normal C++:

Bitmap bmp(panel1->ClientSize.Width, panel1->ClientSize.Height);
panel1->DrawToBitmap(%bmp, panel1->ClientRectangle);
bmp.Save("Capture.bmp");

Upvotes: 2

Related Questions