C# 设置默认图像路径
当图像文件找不到或不存在时,如何设置默认图像? 我犯的错误是 因为名为C# 设置默认图像路径,c#,image,winforms,C#,Image,Winforms,当图像文件找不到或不存在时,如何设置默认图像? 我犯的错误是 因为名为8813.jgp的文件不存在。我想将其默认设置为1.jpg。如何检查其是否存在 我正在使用这个代码 public void profilepicture() { DataTable dtprofile = new DataTable(); DataSet dsprofile = new DataSet(); string path; SqlCommand command =
8813.jgp
的文件不存在。我想将其默认设置为1.jpg
。如何检查其是否存在
我正在使用这个代码
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();
}
您可以使用
System.IO
命名空间中的file.exists()
检查文件是否存在
您可以使用
System.IO
命名空间中的file.exists()
检查文件是否存在
其实很琐碎
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");
}
其实很琐碎
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");
}
我建议您创建一个单独的方法来获取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);
}
}
对不起,我的英语是我建议您创建一个单独的方法来获取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);
}
}
对不起,我的英语是独立应用程序 如果您的应用程序是独立应用程序(仅需要exe,硬盘中不需要其他文件),则可以在资源文件中添加默认映像,并根据需要使用它。下面的堆栈溢出链接解释了如何访问添加到资源文件中的图像 已安装的应用程序 如果应用程序不是独立的应用程序,则可以使用方法或存储默认图像 为了避免文件未找到异常,您可能需要在代码中使用如下内容
string defaultImagePath = Application.StartupPath + "\\1.jpg";
pictureEdit2.Image = Image.FromFile( File.Exists(path) ? path : defaultImagePath) ;
独立应用程序 如果您的应用程序是独立应用程序(仅需要exe,硬盘中不需要其他文件),则可以在资源文件中添加默认映像,并根据需要使用它。下面的堆栈溢出链接解释了如何访问添加到资源文件中的图像 已安装的应用程序 如果应用程序不是独立的应用程序,则可以使用方法或存储默认图像 为了避免文件未找到异常,您可能需要在代码中使用如下内容
string defaultImagePath = Application.StartupPath + "\\1.jpg";
pictureEdit2.Image = Image.FromFile( File.Exists(path) ? path : defaultImagePath) ;
嗯,我不知道,
文件的system.io存在。chill~使用System.IO包含System.IO的名称空间;它存在于程序集mscorlib.dll中,我相信您已经有了!嗯,我不知道,文件的system.io存在。chill~使用System.IO包含System.IO的名称空间;它存在于程序集mscorlib.dll中,我相信您已经有了!