Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

c# - How to capture all the items inside a control?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...