【发布时间】:2011-05-07 20:38:20
【问题描述】:
我有一个问题:
我有 3 个图片框,里面有 3 张不同的图片就像在图片中一样
我可以为 pictureBox3 设置什么,这样两张图片看起来都一样.....
已编辑: 我想把pictureBox3移到pictureBox2上,
所以没有将它们合并到单个图像的选项
【问题讨论】:
标签: c# .net winforms picturebox
我有一个问题:
我有 3 个图片框,里面有 3 张不同的图片就像在图片中一样
我可以为 pictureBox3 设置什么,这样两张图片看起来都一样.....
已编辑: 我想把pictureBox3移到pictureBox2上,
所以没有将它们合并到单个图像的选项
【问题讨论】:
标签: c# .net winforms picturebox
确保pictureBox3 中的图像是透明的。将BackColor 设置为透明。在代码中,将pictureBox3 的Parent 属性设置为pictureBox2。调整pictureBox3 的Location 坐标,因为一旦您更改了Parent,它们将相对于pictureBox2 的坐标。
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);
}
在设计器中你不会看到透明度,但在运行时你会看到。
更新
在图片中,左侧是设计器视图,右侧是运行时版本。
另一个更新
我真的不明白这怎么可能不适合你。我想一定有一些我们正在做的不同的事情。我将描述创建工作样本的具体步骤。如果您遵循完全相同的步骤,我想知道我们是否会得到相同的结果。接下来的步骤描述了要做什么并使用我在网上找到的两张图片。
现在将以下代码放入表单的 OnLoad 事件处理程序中:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.Parent = pictureBox1;
}
就是这样!如果我运行这个程序,我会在另一个图像之上得到一个透明图像。
【讨论】:
pictureBox3.Location.Offset 更改位置,但这不起作用,因为Location 属性返回一个值,而不是引用。如果你想使用Offset,你必须创建一个变量来保存Point,在该变量上使用Offset并将其分配给pictureBox3.Location。
我将添加另一个示例,根据更新后的要求允许移动图像3。
要使其正常工作,请在 Resources\transp.png
中放置一个透明的图像
这对所有三个图像使用相同的图像,但您可以简单地将 image1 和 image2 的 transparentImg 替换为合适的图像。
演示开始后,中间的图像可以拖放到表单周围。
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 - e.X, movingPicturePosition.Y - e.Y);
}
}
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;
}
}
【讨论】:
这段代码可以解决问题:
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中的图像。
【讨论】:
首先,将 PictureBox3 的 BackColor 属性设置为透明。这应该适用于几乎所有情况。
您还应该使用具有透明背景而不是白色的图像,这样您的紫色圆圈周围就不会出现白色边框。 (推荐图片格式:PNG)
更新
根据我收到的回复,将BackColor 设置为透明似乎不起作用。在这种情况下,您最好处理 PictureBox 的 Paint 事件,并以 Albin suggested 自己绘制新图像。
【讨论】:
您可能会通过覆盖 OnPaint 和其他东西来做一些 hack,例如 here。
但我建议将pictureBox2和3中的图片合并为一个图像,然后再将它们显示在一个pictureBox中。
【讨论】: