在img.Image.FromFile()中设置正确的路径;

时间:2016-09-06 06:57:13

标签: c# datagridview

这是我写的代码,在img.Image.FromFile();我写了完整的路径但是需要从文件夹Pics中选择图片。我的意思是我改变DataGridView的选择时所写的路径,我只能在自己的笔记本电脑上看到这些图片。

 private void dataGridViewSearch_SelectionChanged(object sender, EventArgs e)
 {
     if (dataGridViewSearch.SelectedRows.Count == 0) return;
     dataGridViewSearch.Visible = false;

     if (dataGridViewSearch.CurrentCell.ColumnIndex == 0)
     {
         buttonB.Visible = true;
         int n = dataGridViewSearch.CurrentRow.Index;
         string imgName = arr2[n].Pic;

         img = Image.FromFile("C:/Users/baghd/Summer2016/Desktop/1/Pics/" + imgName);
         //img = Image.FromFile("Pictures/" + imgName);

         pictureBox1.Image = img;
         pictureBox1.Visible = true;
     }
     else { }
        //C:/Users/baghd/Summer2016/Desktop/1/
}

1 个答案:

答案 0 :(得分:0)

您可以使用Statis类Enviorment在任何计算机上获取桌面路径:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/1/Pics" + imgName;

当你编写你的路径时,你需要将@放在它前面,因为斜杠

img = Image.FromFile(@"C:/Users/baghd/Summer2016/Desktop/1/Pics/" + imgName);

img = Image.FromFile("C://Users//baghd//Summer2016//Desktop//1//Pics//" + imgName);

希望这会有所帮助......