显示图像从其相应的路径,这是保存在数据库中

本文关键字:保存 存在 数据库 路径 图像 显示图 显示 | 更新日期: 2023-09-27 18:02:27

在我的应用程序中,我已经将图像存储在一个文件夹中,并使用以下代码将其对应的路径保存在数据库中:

if (pictureBox1.Image != null)
{
    string imagepath = pictureBox1.ImageLocation.ToString();
    string picname = imagepath.Substring(imagepath.LastIndexOf(''''));
    string path = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("bin"));
    Bitmap imgImage = new Bitmap(pictureBox1.Image); 
    imgImage.Save(path + "''Image''" + picname + ".jpg");
    location = path + "'''Image'''" + picname;
}
else
    location = "";

我的问题是如何从数据库中保存的相应路径显示picturebox中的图像。在此代码中,字符串变量location保存在数据库中。我正在使用SQL服务器。

显示图像从其相应的路径,这是保存在数据库中

试试这个:

string ImagePath = "";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT photo FROM othersInfo WHERE empID='" + s + "'", con);
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
if(dataReader.HasRows)
{
  dataReader.Read();
  ImagePath = Convert.ToString(dataReader["photo"]);
}
if (ImagePath != "") 
{
  Image image = Image.FromFile(ImagePath)
  pictureBox1.Image = image;
}