2016-09-28 148 views
0

当图像文件未找到或不存在时,如何设置默认图像? 我得到的错误是设置默认图像路径

enter image description here

因为名字8813.jgp文件不存在。我想将其设置为1.jpg。我如何检查它是否存在。

IAM使用此代码

public void profilepicture() 
    { 
    DataTable dtprofile = new DataTable(); 
    DataSet dsprofile = new DataSet(); 
    string path; 
     SqlCommand command = new SqlCommand(); 
     SqlDataAdapter adapter = new SqlDataAdapter(); 
     try 
     { 
      dsprofile.Clear(); 
      dtprofile.Clear(); 
      command.Connection = myConnection; 
      command.CommandText = "//some query//'"; 

      myConnection.Open(); 
      adapter.SelectCommand = command; 
      adapter.Fill(dtprofile); 
      adapter.Fill(dsprofile); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("error" + ex); 
      myConnection.Close(); 
     } 

     path = dsprofile.Tables[0].Rows[0][0].ToString() + "\\" + number + ".jpg"; 
     pictureEdit2.Image = Image.FromFile(path); 

     myConnection.Close(); 
    } 
+0

https://msdn.microsoft.com/en-us/library/system.io.file .exists(v = vs.110).aspx – Bharadwaj

回答

0

它相当琐碎实际上!

path = dsprofile.Tables[0].Rows[0][0].ToString() + "\\" + number + ".jpg"; 
if(File.Exists(path)){ 
    pictureEdit2.Image = Image.FromFile(path); 
} 
else 
{ 
    pictureEdit2.Image = Image.FromFile("NotFound.jpg"); 
} 
+0

,我不知道,'file.exists'的system.io。 chill〜 – chopperfield

+0

使用System.IO包含System.IO的命名空间 ; 它存在于程序集mscorlib.dll中,我相信你已经拥有了! –

0

我建议你创建一个单独的方法来获得SQL表,所以你可以在每一个你需要的方法使用它,那么你使用下面的代码创建资料图片不同的方法:

public DataTable GetSqlTable(string query) 
{ 
    using (SqlConnection dbConnection = new SqlConnection(@"Data Source={ServerName};Initial Catalog={DB_Name};UID={UserID}; Password={PWD}")) 
    { 
     dbConnection.Open(); 
     SqlDataAdapter da = new SqlDataAdapter(query, dbConnection); 
     da.SelectCommand.CommandTimeout = 600000; //optional 
     try 
     { 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 
      da.Update(dt); 
      dbConnection.Close(); 
      return dt; 
     } 
     catch 
     { 
      dbConnection.Close(); 
      return null; 
     } 
    } 
} 

public void profilepicture() 
{ 
    DataTable dt = GetSqlTable("/* some query */"); 
    string number = "some value"; 
    string defaultImg = "defaultImgPath"; 
    if(dt.Rows.Count > 0) 
    { 
     string path = dt.Rows[0][0].ToString() + "\\" + number + ".jpg"; 
     pictureEdit2.Image = Image.FromFile(File.Exists(path) ? path : defaultImg); 
    } 
    else 
    { 
     pictureEdit2.Image = Image.FromFile(defaultImg); 
    } 
} 

对不起,我的英语

0

独立应用程序

如果你的应用程序是斯坦dalone应用程序(只需要exe文件而不需要硬盘中的其他文件),您可以将默认图像添加到资源文件中并根据需要使用它。下面的堆栈溢出链接介绍如何访问添加图片到一个资源文件Load image from resources area of project in C#

pictureEdit2.Image = File.Exists(path) ? Image.FromFile(path) : Resources.myImage; 

安装的应用程序

如果您的应用程序是不是一个独立的应用程序,你可以使用Environment.GetFolderPath方法或Application.StartupPath存储默认图片。

为了避免文件未发现异常,则可能需要使用File.Exists在你的代码类似下面

string defaultImagePath = Application.StartupPath + "\\1.jpg"; 
pictureEdit2.Image = Image.FromFile(File.Exists(path) ? path : defaultImagePath) ;