【问题标题】:Loading PictureBox Image From Database从数据库加载 PictureBox 图像
【发布时间】:2012-05-04 18:56:24
【问题描述】:

我正在尝试将图像从数据库加载到PictureBox。我使用以下这些代码将它们加载到我的图片中。我已经写了一些代码,但不知道我应该怎么做才能继续。

任何帮助将不胜感激。

private void button1_Click(object sender, EventArgs e)
    {
        sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
        cmd = new SqlCommand();
        cmd.Connection = sql;
        cmd.CommandText = ("select Image from Entry where EntryID =@EntryID");
        cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
    }

【问题讨论】:

    标签: c# sql-server


    【解决方案1】:

    在 button1_Click 中继续这样的操作:

    // Your code first, here.
    
    var da = new SqlDataAdapter(cmd);
    var ds = new DataSet();
    da.Fill(ds, "Images");
    int count = ds.Tables["Images"].Rows.Count;
    
    if (count > 0)
    { 
        var data = (Byte[])ds.Tables["Images"].Rows[count - 1]["Image"];
        var stream = new MemoryStream(data);
        pictureBox1.Image = Image.FromStream(stream);
    } 
    

    【讨论】:

    • 在我的数据库中,我将图像列标记为允许空值。但是当我使用这些代码时,如果行中没有图片,我会遇到错误
    • @aliprogrammer:这与我之前在网址中发布的答案几乎相同(请参阅参考中的示例编号 7)
    • @aliprogrammer 我只给了你一些伪代码来向你展示一种将图像从数据库加载到 PictureBox 的方法。您需要针对您的代码进行自己的错误处理。现在,我希望否决票不是因为我没有错误处理就回答了,因为那会有点错误=)
    • @Mario 我没有投票给你。我投票赞成。好像其他人投票给你了。
    • @aliprogrammer 啊,好的。可惜投反对票的人没有给出解释。好吧好吧。
    【解决方案2】:

    假设我们有一个简单的数据库,其中包含一个名为 BLOBTest 的表:

    CREATE TABLE BLOBTest
    (
    BLOBID INT IDENTITY NOT NULL,
    BLOBData IMAGE NOT NULL
    )
    

    我们可以通过以下方式检索图像编码:

    try
    {
        SqlConnection cn = new SqlConnection(strCn);
        cn.Open();
    
        //Retrieve BLOB from database into DataSet.
        SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);   
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "BLOBTest");
        int c = ds.Tables["BLOBTest"].Rows.Count;
    
        if(c>0)
        {   //BLOB is read into Byte array, then used to construct MemoryStream,
            //then passed to PictureBox.
            Byte[] byteBLOBData =  new Byte[0];
            byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
            MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
            pictureBox1.Image= Image.FromStream(stmBLOBData);
        } 
        cn.Close();
    }
    catch(Exception ex)
    {MessageBox.Show(ex.Message);}
    

    此代码将数据库中BLOBTest 表中的行检索到DataSet,将最近添加的图像复制到Byte 数组中,然后复制到MemoryStream 对象中,然后加载@987654329 @ 到PictureBox 控件的Image 属性中。

    完整参考指南:

    http://support.microsoft.com/kb/317701

    【讨论】:

      【解决方案3】:
      private void btnShowImage_Click(object sender, EventArgs e)
      {
          string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
          Con = new OleDbConnection(@constr);
          Con.Open();
          Com = new OleDbCommand();
          Com.Connection = Con;     
          Com.CommandText = "SELECT Photo FROM PatientImages WHERE Patient_Id =  " + val + " ";
          OleDbDataReader reader = Com.ExecuteReader();
          if (reader.Read())
          {
              byte[] picbyte = reader["Photo"] as byte[] ?? null;
              if (picbyte != null)
              {
                  MemoryStream mstream = new MemoryStream(picbyte);
                  pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
              {
              System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-03
        • 1970-01-01
        • 1970-01-01
        • 2012-08-17
        • 2015-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多