Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Suppose there is a picturexox on the panel and there is a datagridview on the picturebox.

I use the following code to capture the panel:

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(@"test.jpg");

But it seems that only the picturebox has been captured, the datagridview on it is missing.

How can capture the picturebox together with the datagridview?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
188 views
Welcome To Ask or Share your Answers For Others

1 Answer

The DrawToBitmap method will capture the graphics drawn in the Paint event and all nested controls. If one is missing it isn't nested either directly of indirectly. Here is how this probably happened:

The designer behaviour of Panels and PictureBoxes is different.

Panels, like GroupBoxes, TabPages and a few more are container controls.

This means that any control you drag onto them with your mouse gets nested in them.

PictureBox is not a Container, like, say a Button or a Label..

To nest your DataGridView in the PictureBox you have a choice of

  • moving it there with the keyboard (!)
  • nesting it in code ( yourDGV.Parent = yourPBox1;)

Do test the success of the keyboard way by moving the PictureBox to see if the DGV moves with it..

Update:

  • If you nest the DGV in the PictureBox, which itself is nested in the Panel, all will be well and the result will looks just like what you see.

  • However, if you nest both DGV and PictureBox in the Panel and merely let them overlap, a strange bug will appear: Multiple overlapping controls are drawn in reverse, See here for a post and a link about this issue, which is documented

Controls inside containers are rendered in reverse order.

but obviously still a bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...