我有两个图片盒在面板上的两个独立的位置,这将成为隐藏后,一定的时间。我想将picturebox背景图像绘制到面板中picturebox控件所在的确切位置。我看过MSDN库,但我似乎找不到如何做到这一点。
谢谢你的帮助
发布于 2012-07-02 22:40:15
您可以执行类似的操作:
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-02 22:37:09
我会通过在与原始图片相同的位置创建另外两个图片框来实现这一点,但其中没有任何图片。这样一来,它们将始终与原件处于同一位置。您将副本保留为空,这样它们就可以显示背景。
https://stackoverflow.com/questions/11295515
复制相似问题