You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从文件夹将图片显示到PictureBox?加载失败问题排查

问题:PictureBox加载图片显示红叉解决办法

我已将图片路径存储在NewImage(varchar类型)列中,但尝试从Images文件夹加载图片到PictureBox时,控件显示带红叉的空白框。

存储路径的列截图:
Storing image path into NewImage column

Images目录截图:
Storing images in Images directory

以下是我尝试从Images目录检索图片但未生效的代码:

private void Quiz_Load(object sender, EventArgs e)
{
    if (pictureBox1.Image != null)
    {
        pictureBox1.Image.Dispose();
        pictureBox1.Image = null;       
    }

    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
   
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT NewImage FROM tblQuestion WHERE QuestionId =1", conn))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    conn.Open();
                    sda.Fill(dt);
                    
                    pictureBox1.ImageLocation = (@"\Images");
                   
                    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    conn.Close();
                }
            }
        }
    }
}

输出效果截图:
Output:


问题根源

你的代码仅指定了文件夹路径\Images,没有拼接从数据库取出的具体图片文件名,PictureBox无法定位到单个图片文件,因此显示红叉。

修复方案

修正后的代码

private void Quiz_Load(object sender, EventArgs e)
{
    if (pictureBox1.Image != null)
    {
        pictureBox1.Image.Dispose();
        pictureBox1.Image = null;       
    }

    string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
   
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT NewImage FROM tblQuestion WHERE QuestionId =1", conn))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    conn.Open();
                    sda.Fill(dt);
                    
                    // 读取数据库中存储的图片文件名
                    string imageFileName = dt.Rows[0]["NewImage"].ToString();
                    // 拼接完整图片路径(推荐用Path.Combine避免路径分隔符问题)
                    // 假设Images文件夹在程序运行目录下,若为绝对路径可替换成对应路径
                    string fullImagePath = Path.Combine(Application.StartupPath, @"Images", imageFileName);
                    
                    pictureBox1.ImageLocation = fullImagePath;
                    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    conn.Close();
                }
            }
        }
    }
}

额外检查项

  • 确认Images文件夹存在于程序运行目录(如bin/Debug或bin/Release)下
  • 核对数据库NewImage列的文件名与实际图片文件名完全一致(包括大小写、后缀)
  • 确保图片文件未被占用,且程序拥有该文件的读取权限

内容的提问来源于stack exchange,提问作者Avinash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:45:38