一个图片问题问题、图片

2023-09-02 21:08:21 作者:马尾辫子

我有一个问题:

我有3个图片框有3种不同的图像作为在图像

I have 3 picture boxes with 3 different images as in Image

我可以设置为 pictureBox3 所以无论图像看起来一样......

what can i set to pictureBox3 so both images look same.....

编辑: 我想移动pictureBox3上pictureBox2,

EDITED: I want to move pictureBox3 on pictureBox2,

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

So there is no Option to merge them to single image

推荐答案

我将添加另一个例子,根据更新的要求,允许移动的Image3。 为了得到它的工作,把透明的图像资源 transp.png 它使用了相同的图像的所有三个图像,但你可以简单地取代transparentImg为与此搜索IMAGE2到合适的图像。

I'll add another example that according to the updated requirement allows for moving image3. To get it working, put an image with transparency in Resourcestransp.png This uses the same image for all three images, but you can simply replace transparentImg for image1 and image2 to suitable images.

在演示一开始中间的图像可以拖动,周围丢弃的形式。

Once the demo is started the middle image can be dragged-dropped around the form.

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;
    }
}
 
精彩推荐
图片推荐