C#:获取图片框的文件路径 onclick

R.V*_*tor 1 c# user-interface user-controls picturebox winforms

我创建了一个循环文件​​夹并检索每个图像文件并在表单上绘制一个图片框的函数。这是功能:

    private void Create_Controls(string Img_path)
    {
        PictureBox p = new PictureBox();
        p.Size = new Size(138, 100);
        p.Location = new Point(6, 6);
        p.BackgroundImage = Image.FromFile(Img_path);
        p.BackgroundImageLayout = ImageLayout.Stretch;

        this.Controls.Add(p);
    }
Run Code Online (Sandbox Code Playgroud)

所以我需要做的是:每当我点击表单上的任何图片框时,都会弹出一条带有图像文件路径的消息。

所以我想添加一个自定义事件:

p.Click += delegate { Pop_Up(); };
Run Code Online (Sandbox Code Playgroud)

    private void Pop_Up()
    {
        /* POP UP MESSAGE WITH Picturebox image file path*/
    }
Run Code Online (Sandbox Code Playgroud)

Hat*_*oft 5

您需要使用PictureBox 的ImageLocation属性。该属性获取或设置要在 PictureBox 中显示的图像的路径或 URL。