Get Image Path in C#: Simple Guide

There are multiple ways to retrieve the image path in C#, here is one commonly used method:

// 创建一个OpenFileDialog对象
OpenFileDialog openFileDialog1 = new OpenFileDialog();

// 设置过滤器,只允许选择图片文件
openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";

// 如果用户点击了“确定”按钮
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    // 获取选择的文件路径
    string imagePath = openFileDialog1.FileName;
    
    // 可以通过imagePath来获取图片路径
}

The above code uses the OpenFileDialog dialog box to allow users to select an image file and retrieve the selected file path. You can also obtain the image path through other methods, such as directly specifying the image file path or retrieving the image path from a database.

bannerAds