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…
Use
panel1->ClientRectangleinstead ofpanel1->Bounds, andpanel1->ClientSizerather thanpanel1->Widthandpanel1->Height.Additionally, don’t use
gcnewunless you have to – your code, as-is, is not exception-safe. Use RAII instead, just like normal C++: