vb.net 图像的面板控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13184082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Panel Controls to Image
提问by Eric F
I am trying to save the contents of a panel to an image which is displayed on a picturebox. I am using the drawtobitmap method as shown below:
我正在尝试将面板的内容保存到显示在图片框上的图像中。我正在使用 drawtobitmap 方法,如下所示:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle)
PictureBox1.BackgroundImage = bmp
End Sub
The picturebox displays the image however the draw order is incorrect.
图片框显示图像,但绘制顺序不正确。


The box on the left is the panel with 3 buttons. The box on the right is the picturebox. Notice how the ordering differs. Does anyone have any suggestion of how to fix this so the ordering is the same as it appears on the panel?
左边的框是带有3个按钮的面板。右边的框是图片框。注意顺序的不同。有没有人对如何解决这个问题有任何建议,以便订购与面板上显示的相同?
采纳答案by Eric F
Jon B. You were correct to reverse the z order but I found this method below to do that for me instead which seems to work. Thank you for your help! :)
Jon B. 你反转 z 顺序是正确的,但我发现下面的这个方法对我来说似乎有效。感谢您的帮助!:)
For Each ctl As Control In Me.Controls.OfType(Of Control).OrderBy(Function(c) Me.Controls.GetChildIndex(c))
ctl.BringToFront()
Next
回答by Jon B
I did this in c#, but you should be able to translate:
我在 c# 中做了这个,但你应该能够翻译:
private void button3_Click(object sender, EventArgs e)
{
ReverseControls(panel1);
var bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
pictureBox1.BackgroundImage = bmp;
ReverseControls(panel1);
}
static void ReverseControls(Panel panel)
{
var controls = panel.Controls.Cast<Control>().Reverse().ToArray();
panel.Controls.Clear();
panel.Controls.AddRange(controls);
}
Since it's drawing the controls in the reverse order, I just get a reversed list of controls and remove/readd them. Then un-reverse them after drawing the bitmap.
由于它以相反的顺序绘制控件,我只是得到一个反向的控件列表并删除/读取它们。然后在绘制位图后取消反转它们。
回答by stockvu
This works for me in VB.net
这在 VB.net 中对我有用
' flip all Controls to reverse order
For I As Int32 = MyPanel.Controls.Count - 1 To 0 Step -1
MyPanel.Controls(I).SendToBack()
Next
'create Bitmap of Panel for Printer
MyPanel.DrawToBitmap(bm, r)
' flip Controls again to restore original order on Screen
For I As Int32 = MyPanel.Controls.Count- 1 To 0 Step -1
MyPanel.Controls(I).SendToBack()
Next

