C#怎么获取已经加载的picturebox的文件名

我在一个button1的单机事件中从文件夹中载入了picturebox1的图片,然后怎么在button2的单机事件中获取picturebox1的图片的文件名

img

通用PictureBox的ImageLocation即可读取到指定PictureBox的图片路径,示例如下:

var imageLocation = pictureBox1.ImageLocation;

但是,你提供的代码片段是设置的pictureBox1.BackgroundImage = ...,所以,直接读取ImageLocation是空的。

可以将图片1的背景图片路径设置到Tag属性中,然后,图片2读取图片1的Tag属性的值即可,示例如下:

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(pictureBox1.Tag.ToString());
    pictureBox2.Image = Image.FromFile(pictureBox1.Tag.ToString());
}

private void Form2_Load(object sender, EventArgs e)
{
    var path = @"D:\tmp\2.jpg";
    pictureBox1.BackgroundImage = Image.FromFile(path);
    pictureBox1.Tag = path;
}