0

我在两个不同位置的面板上有 2 个图片框,这些图片框将在一段时间后隐藏。我想在图片框控件所在的确切位置将图片框背景图像绘制到面板上。我查看了 MSDN 库,但似乎不知道如何执行此操作。

谢谢你的帮助

4

2 回答 2

0

我会通过在与原件相同的位置创建另外 2 个图片框来做到这一点,但其中没有任何图片。这样,它们将始终与原件位于同一位置。您将欺骗者留空,以便它们显示背景。

于 2012-07-02T14:37:09.367 回答
0

你可以做类似这样的事情:

Bitmap bitmap = new Bitmap(panel1.Size.Width, panel1.Size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawImage(pictureBox1.BackgroundImage, new Rectangle(pictureBox1.Location, pictureBox1.Size));
    g.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));
    g.Flush();
}

pictureBox1.Visible = false;
pictureBox2.Visible = false;
panel1.BackgroundImage = bitmap;

或者你可以尝试使用这个:

public class PanelEx : Panel
{
    public PictureBox PictureBox1 { get; set; }
    public PictureBox PictureBox2 { get; set; }
    public bool IsBackgroundDrawn { get; set; }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        if (!IsBackgroundDrawn)
        {
            IsBackgroundDrawn = true;
            base.OnPaintBackground(e);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (PictureBox1 != null && PictureBox2 != null && !IsBackgroundDrawn)
        {
            Bitmap bitmap = new Bitmap(this.Size.Width, this.Size.Height);
            e.Graphics.DrawImage(PictureBox1.BackgroundImage, new Rectangle(PictureBox1.Location, PictureBox1.Size));
            e.Graphics.DrawImage(PictureBox2.BackgroundImage, new Rectangle(PictureBox2.Location, PictureBox2.Size));
            e.Graphics.Flush();

            PictureBox1.Visible = false;
            PictureBox2.Visible = false;
            this.BackgroundImage = bitmap;
            IsBackgroundDrawn = false;
        }
    }
}
于 2012-07-02T14:40:15.037 回答