Jav*_*ram 11 .net c# picturebox winforms
我有个问题:
我有3个图片框,有3个不同的图像,如图像
我可以设置为pictureBox3所以两个图像看起来相同.....

编辑: 我想在pictureBox2上移动pictureBox3,
因此没有选项将它们合并到单个图像
确保图像pictureBox3是透明的.将其设置BackColor为透明.在代码中,设置Parent的属性pictureBox3是pictureBox2.调整Location坐标,pictureBox3因为它们将相对于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);
}
Run Code Online (Sandbox Code Playgroud)
在设计师中你不会看到透明度,但在运行时你会看到.
更新
在图像中,左侧显示设计器视图,右侧是运行时版本.

另一个更新
我真的不明白这对你有什么用.我想必须有一些我们正在做的事情.我将描述创建工作样本的确切步骤.如果你按照完全相同的步骤,我想知道我们是否会得到相同的结果.接下来的步骤描述了要做什么,并使用我在网上找到的两个图像.
现在将以下代码放在窗体的OnLoad事件处理程序中:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.Parent = pictureBox1;
}
Run Code Online (Sandbox Code Playgroud)而已!如果我运行这个程序,我会在另一个图像上面得到一个透明图像.
我将添加另一个示例,根据更新的要求允许移动image3.
为了使其工作,将透明图像放入其中.Resources\transp.png
这对所有三个图像使用相同的图像,但您可以简单地将transparentImg替换为image1,将image2替换为合适的图像.
演示开始后,可以在表单周围拖放中间图像.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18454 次 |
| 最近记录: |