通过单击在位置 480,380 上的另一个图片框图像上设置一个图片框图像

本文关键字:图像 框图 设置 一个 另一个 单击 位置 | 更新日期: 2023-09-27 18:31:59

我想通过单击图像在位置 480,380 上的另一个图片框图像上设置该图像。我不知道如何设置以及在哪里编写代码。就像 neckelss 会通过单击在客户图像颈部添加一样。它很紧急,我会感谢您。

通过单击在位置 480,380 上的另一个图片框图像上设置一个图片框图像

这不一定是"最佳"答案,但如果您使用的是可视化快速 IDE,并且您说您很着急,这可能是最简单的答案。

1)在表单中创建一个图片框(pictureBox1或默认情况下称为的任何名称)。
2) 双击表单以创建表单加载功能。 它应该看起来像这样:

    private void Form1_Load(object sender, EventArgs e) {
    }

3)向其添加代码以创建另一个图片框并将其添加到您的第一个图片框中:

    PictureBox ChildBox;
    private void Form1_Load(object sender, EventArgs e) {
        ChildBox = new PictureBox();
        ChildBox.Visible = false;
        ChildBox.Location = new Point(0, 0); // change this to your coordinates, 480 by 380
        // the next 2 lines are just so that you can see the changes
        ChildBox.BackColor = Color.Red;
        pictureBox1.BackColor = Color.Blue;
        pictureBox1.Controls.Add(ChildBox);
    }

4) 双击表单中的 PictureBox1 以创建以下存根:

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

5) 将其更改为

    private void pictureBox1_Click(object sender, EventArgs e) {
        ChildBox.Visible = true;
    }

这将使图片框"出现"在您想要的坐标处。

一个简单的例子

private List<Tuple<Image, int, int>> images = new List<Tuple<Image, int, int>>();
private void Form1_Load(object sender, EventArgs e)
{
    //load the customer image
    this.picBoxTarget.BackgroundImage = Image.FromFile(...);
    //load the necklaces image
        this.picBoxSource.Image = Image.FromFile(...);
    }
    private void picBoxSource_Click(object sender, EventArgs e)
    {
        if (this.picBoxSource.Image == null)
            return;
        //when click the picBoxSource, add the image to list
        //(you may need to check whether there is another one necklace in the list, if not allowed to wear two)
        this.images.Add(Tuple.Create(this.picBoxSource.Image, 480, 380));
        //and make the picBoxTarget redraw
        this.picBoxTarget.Invalidate();
    }
    private void picBoxTarget_Paint(object sender, PaintEventArgs e)
    {
        foreach (var img in this.images)
            e.Graphics.DrawImage(img.Item1, img.Item2, img.Item3);
    }