2

我正在尝试将面板的内容保存到图片框中显示的图像中。我正在使用 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

图片框显示图像,但绘制顺序不正确。

在此处输入图像描述

左侧的框是带有 3 个按钮的面板。右边的框是图片框。注意顺序的不同。是否有人对如何解决此问题有任何建议,以使顺序与面板上显示的顺序相同?

4

3 回答 3

2

我在 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);
}

由于它以相反的顺序绘制控件,我只得到一个相反的控件列表并删除/读取它们。然后在绘制位图后取消反转它们。

于 2012-11-01T19:55:43.293 回答
1

Jon B. 你颠倒 z 顺序是正确的,但我在下面找到了这个方法来为我做这件事,而不是似乎有效。感谢您的帮助!:)

For Each ctl As Control In Me.Controls.OfType(Of Control).OrderBy(Function(c) Me.Controls.GetChildIndex(c))
ctl.BringToFront()
Next
于 2012-11-07T22:16:25.533 回答
0

这在 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
于 2017-08-14T19:10:23.997 回答