【问题标题】:How to display image into picture box using image path in database如何使用数据库中的图像路径将图像显示到图片框中
【发布时间】:2017-06-08 15:55:17
【问题描述】:

如何使用数据库中的图像路径将图像检索到图片框中?在此之前,我从共享文件夹中检索,但它使我的系统在加载图像时变慢。然后我改变了我想使用存储在 MSSQL 数据库中的图像路径将图像检索到图片框中的方法。希望它不会减慢我的系统。任何人请帮助我。这是我的代码,但无法检索图像。

private void textBoxEmplNo_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (textBoxEmplNo.Text != "")
            {

                string selectSql = "select a.name, a.empno, a.workno, a.icnum, a.passport, a.deptno, a.section, a.designation, b.path from m_employee a inner join m_emp_photo b on b.empno=a.empno where a.empno= @empno";

                SqlCommand cmd = new SqlCommand(selectSql, con);

                cmd.Parameters.AddWithValue("@empno", textBoxEmplNo.Text);

                bool isDataFound = false;

                try
                {

                    con.Open();

                    using (SqlDataReader read = cmd.ExecuteReader())
                    {
                        while (read.Read())
                        {
                            isDataFound = true;

                            textBoxWorkNo.Text = (read["workno"].ToString());
                            textBoxName.Text = (read["name"].ToString());
                            textBoxICPass.Text = (read["icnum"].ToString());
                            textBoxPassport.Text = (read["passport"].ToString());
                            textBoxDept.Text = (read["deptno"].ToString());
                            textBoxSection.Text = (read["section"].ToString());
                            textBoxDesignation.Text = (read["designation"].ToString());

                            pictureBox1.Image = Image.FromFile("@path");
                        }
                    }

【问题讨论】:

    标签: c# image visual-studio windows-forms-designer


    【解决方案1】:

    pictureBox1.Image = Image.FromFile("" + read["path"].ToString());

                pictureBox1.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    

    【讨论】:

    • 你能解释一下你的解决方案在做什么,而不是仅仅粘贴一段代码。
    • 祝你成功@Miza ;)
    • @CagdasBsn 谢谢。现在我有一些问题,如果数据库中没有图像路径,则员工详细信息将显示“未找到结果”。我该如何解决?我想在哪里找不到图像,其他文本框仍然从数据库中获取值。 :(
    • if (String.IsNullOrEmpty(pictureBox1.Image)) { pictureBox1.Image="no result found";请尝试! @米扎
    • @CagdasBsn 这不是我想要的。我已经尝试过您以前的方法来加载图像及其工作。但是我有问题,如果数据库中没有图像路径,它在所有文本框中都没有显示任何结果。即使图像路径在数据库中没有,我也希望其他文本框仍然获得价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2021-03-05
    • 2018-11-08
    相关资源
    最近更新 更多