我在两个不同位置的面板上有2个图片框,在一段时间后会变为隐藏。我想在图片框控件所在的确切位置将图片框背景图像绘制到面板上。我查看了MSDN库,但似乎无法找到如何执行此操作。
感谢您的帮助
答案 0 :(得分:0)
我会通过在与原件相同的位置创建另外2个图片框但不包含任何图片来完成此操作。这样他们将始终与原件处于同一位置。你将骰子留空,以便显示背景。
答案 1 :(得分: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;
}
}
}