【问题标题】:How to save and retrieve binary image data to picture box in winforms using C#如何使用C#在winforms中将二进制图像数据保存和检索到图片框
【发布时间】:2014-12-23 13:11:07
【问题描述】:

如何使用C#在Winforms中将二进制图像数据保存和检索到图片框?

我试过了

ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);

SqlCommand cmd1 = new SqlCommand("insert into Registration(Photo)values('"+photo_aray+"')

正在保存,但在尝试检索时,出现“参数无效”错误。

我已经尝试过这个搜索

photo_aray = (byte[])dr[0];
MemoryStream ms = new MemoryStream(photo_aray);
pictureBox1.Image = Image.FromStream(ms);

在最后一行我收到错误,如何排序?我的Photo 的数据库列是Image

【问题讨论】:

  • 你能用错误信息更新问题吗?
  • 您的 DataTable Registration 上的 PHOTO 列的数据类型是什么?您是否使用 nvarchar 存储二进制数据?
  • ntexttextimage 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)varchar(max)varbinary(max)See details here
  • @Steve 我在 sql server 中的数据类型是图片类型

标签: sql image winforms picturebox image-uploading


【解决方案1】:

首先要确定。您的列 PHOTO 应在表中声明为 varbinary(MAX)
这是存储二进制值的推荐数据类型。

现在要保存您的 PictureBox 图像,您需要一个稍微不同的代码:

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
byte[] imgData = new byte[ms.Length];
ms.Position = 0;
ms.Read(imgData, 0, imgData.Length);

// Using a parameterized query passing the binary data as binary not as chars...
string cmdText = "INSERT INTO Registration(Photo) VALUES (@picBits)";
using(SqlConnection con = new SqlConnection(......))
using(SqlCommand cmd = new SqlCommand(cmdText, con)) 
{ 
con.Open(); 
    cmd.Parameters.Add(new SqlParameter {ParameterName = "@picBits", 
                                        SqlDbType = SqlDbType.VarBinary, 
                                        Size = imgData.Length,
                                        Value = imgData});
    cmd.ExecuteNonQuery();
}

要从 PictureBox 内的数据库中重新加载图像,您需要这样的东西

byte[] imgData;

// Of course this command should be adapted to your context
string cmdText = "SELECT Photo from Registration where id = 1";

using(SqlConnection con = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(cmdText, con)) 
{ 
   con.Open(); 
   using(SqlDataReader reader = pvCommand.ExecuteReader())
   {
       if(reader.Read())
       {
           // First call to get the length of binary data that we want to read back
           long bufLength = reader.GetBytes(0, 0, null, 0, 0);

           // Now allocate a buffer big enough to receive the bits...
           imgData = new byte[bufLength];

           // Get all bytes from the reader
           reader.GetBytes(0,0,imgData, 0, (int)bufLength);

           // Transfer everything to the Image property of the picture box....
           MemoryStream ms = new MemoryStream(imgData);
           ms.Position = 0;
           pictureBox1.Image = Image.FromStream(ms);
       }    
   }
}

【讨论】:

    【解决方案2】:

    这是我的问题,在我无法将 DGV 上的图像的数据值放到图片框上之前,问题是我像这样将它输入到数据库中 cmd.Parameters.AddWithValue("pic", ba );

    private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)

            byte[] imgData;
    
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\naedi\Documents\Information.accdb");
            OleDbCommand cmd = new OleDbCommand("Select Pic From Info where Firstname = '"+textBox1.Text+"'",con);
            con.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            long bufLength = reader.GetBytes(0, 0, null, 0, 0);
            imgData = new byte[bufLength];
            reader.GetBytes(0, 0, imgData, 0, (int)bufLength);
            MemoryStream ms = new MemoryStream(imgData);
            ms.Position = 0;
            pictureBox1.Image = Image.FromStream(ms);
            reader.Close();
    
            if (e.RowIndex <= 10)
            {
    
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
    
                dateTimePickerHire.Text = row.Cells["Date"].Value.ToString();
                txtLastName.Text = row.Cells["LastName"].Value.ToString();
                txtFirstName.Text = row.Cells["FirstName"].Value.ToString();
                txtMiddleName.Text = row.Cells["MiddleName"].Value.ToString();
                txtStreet.Text = row.Cells["StreetAddress"].Value.ToString();
                txtState.Text = row.Cells["City"].Value.ToString();
                txtProvince.Text = row.Cells["Province"].Value.ToString();
                dateTimePickerBday.Text = row.Cells["Birthday"].Value.ToString();
                comboGender.Text = row.Cells["Gender"].Value.ToString();
                comboCivil.Text = row.Cells["CivilStatus"].Value.ToString();
                txtReli.Text = row.Cells["Religion"].Value.ToString();
                comboContract.Text = row.Cells["Contract"].Value.ToString();
                txtDept.Text = row.Cells["Department"].Value.ToString();
                txtPosition.Text = row.Cells["Position"].Value.ToString();
                txtTIn.Text = row.Cells["TIN"].Value.ToString();
                txtSSS.Text = row.Cells["GSISsss"].Value.ToString();
                txtPagibig.Text = row.Cells["Pagibig"].Value.ToString();
                txtPhil.Text = row.Cells["Philhealth"].Value.ToString();
        }
    
        }
    

    但是有了这个大的帮助,我的问题终于解决了。

    这是不可能的,但我只想对史蒂夫说声谢谢,我这两天的问题已经解决了 :) 我无法发表评论,因为我还没有 50 声望。呵呵

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      相关资源
      最近更新 更多