一个PictureBox的问题

我有个问题:

我有3个图片与3个不同的图像在图像

我可以设置为pictureBox3,所以两个图像看起来一样…..

替代文字

编辑:我想在pictureBox2上移动pictureBox3,

所以没有选项将它们合并到单个图像

我将增加另一个例子,根据更新的要求允许移动image3。
为了让它工作,请在Resources\transp.png放置一个透明的图像
这对所有三个图像使用相同的图像,但是您可以简单地将image1和image2的transparentImgreplace为合适的图像。

一旦演示开始,中间的图像可以在表单上拖放。

 public partial class Form1 : Form { private readonly Image transparentImg; // The transparent image private bool isMoving = false; // true while dragging the image private Point movingPicturePosition = new Point(80, 20); // the position of the moving image private Point offset; // mouse position inside the moving image while dragging public Form1() { InitializeComponent(); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(231, 235); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); this.Controls.Add(this.pictureBox1); transparentImg = Image.FromFile("..\\..\\Resources\\transp.png"); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { var g = e.Graphics; g.DrawImageUnscaled(transparentImg, new Point(20, 20)); // image1 g.DrawImageUnscaled(transparentImg, new Point(140, 20)); // image2 g.DrawImageUnscaled(transparentImg, movingPicturePosition); // image3 } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { var r = new Rectangle(movingPicturePosition, transparentImg.Size); if (r.Contains(e.Location)) { isMoving = true; offset = new Point(movingPicturePosition.X - eX, movingPicturePosition.Y - eY); } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (isMoving) { movingPicturePosition = e.Location; movingPicturePosition.Offset(offset); pictureBox1.Invalidate(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { isMoving = false; } } 

确保pictureBox3的图像是透明的。 将BackColor设置为透明。 在代码中,将pictureBox3Parent属性设置为pictureBox2 。 调整pictureBox3Location坐标,因为它们将相对于pictureBox2的坐标,一旦您更改了Parent

  private void Form1_Load(object sender, EventArgs e) { pictureBox3.Parent = pictureBox2; pictureBox3.Location = new Point( pictureBox3.Location.X - pictureBox2.Location.X, pictureBox3.Location.Y - pictureBox2.Location.Y); } 

在devise师你不会看到透明度,但在运行时你会。

更新

在图像中,左侧显示devise器视图,右侧是运行时版本。 左:设计器视图,右:运行时看起来如何

另一个更新

我真的不明白这可能不适合你。 我想我们应该有一些不同的事情。 我将描述创build工作示例的确切步骤。 如果您遵循完全相同的步骤,我想知道我们是否会得到相同的结果。 接下来的步骤描述了怎么做,并使用我在网上find的两个图像。

  • 使用Visual Studio 2008,使用模板Windows窗体应用程序创build一个新项目。 确保项目的目标是.NET Framework 3.5。
  • 将表单的大小设置为457; 483。
  • 将一个PictureBox控件拖到窗体上。 将其位置设置为0; 0,其大小设置为449; 449。
  • 单击除Image属性以外的省略号,单击导入…button并导入图像: http://a.dryicons.com/files/graphics_previews/retro_blue_background.jpg (只需在“文件名”文本框中键入URL并单击打开)。 然后点击确定使用图像。
  • 将另一个PictureBox拖到窗体上,将其位置设置为0;将其大小设置为256; 256。 还将其BackColor属性设置为透明。
  • 使用与上述相同的方法,导入图像http://www.axdn.com/redist/axiw_i.png这是一个透明的图像。;
  • 现在将下面的代码放在窗体的OnLoad事件处理程序中:

     private void Form1_Load(object sender, EventArgs e) { pictureBox2.Parent = pictureBox1; } 

而已! 如果我运行这个程序,我会在另一个图像上得到一个透明的图像。

对于初学者,将PictureBox3的BackColor属性设置为透明。 这应该在几乎所有情况下都起作用。

你也应该使用透明背景的图像,而不是白色,所以你的紫色圆圈周围没有白色边框。 (推荐图片格式:PNG)


更新
在得到的回复之后,出现将BackColor设置为“透明”不起作用。 在这种情况下,最好是处理PictureBox的Paint事件,并按照Albin的build议自己绘制新图像。

这个代码将做的伎俩:

 using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { g.DrawImage(pictureBox2.Image, (int)((pictureBox1.Image.Width - pictureBox2.Image.Width) / 2), (int)((pictureBox1.Image.Height - pictureBox2.Image.Height) / 2)); g.Save(); pictureBox1.Refresh(); } 

它将在pictureBox1的现有图像上从pictureBox2中绘制图像。

你可以通过覆盖OnPaint和东西来做一些破解,例如这里 。

但我build议将pictureBox2和3中的图片合并成一个图片,然后再将它们显示在一个图片框中。