使用C#将ByteArray类型的图像文件加载到图片框

时间:2014-08-02 04:00:27

标签: c# datagridview

我有一个byte[]类型的图片文件,保存在sql image字段中,图片将显示在DGV(DataGridView)中,现在我尝试在currentRow(DataGridViewRow)对象的窗体上显示图片框中的图像我收到错误"无法将System.String类型的对象转换为System.Byte[]类型。",我该怎么做?有人可以帮忙!谢谢

private void DGV_SelectionChanged(object sender, EventArgs e)  
    {  
        private DataGridViewRow currentRow = new DataGridViewRow();
        //Get current row object
        currentRow = DGV.Rows[DGV.CurrentRow.Index];
        //Display Fields Detail on the form
        DisplayFields(currentRow);
    }

private void DisplayFields(DataGridViewRow row)  
    {  
        this.tbxItemGUID.Text = row.Cells[0].Value.ToString();
        //row.Celles[1] contained byte[] image file which saved in sql image field
        //how can I display image in picture box
        this.pictureBox.image = (byte[])row.Cells[1].Value;
    }

1 个答案:

答案 0 :(得分:0)

谢谢大家,我已经解决了这个问题,这就是我做的事情..请告诉我是否有更好的方法。

private void DGV_SelectionChanged(object sender,EventArgs e)
    {

    private DataGridViewRow currentRow = new DataGridViewRow();
    //Get current row object
    currentRow = DGV.Rows[DGV.CurrentRow.Index];
    //Display Fields Detail on the form
    DisplayFields(currentRow);
}

private void DisplayFields(DataGridViewRow row)
    {

    this.tbxItemGUID.Text = row.Cells[0].Value.ToString();  
    byte[] picture = new byte[0];
    picture = (byte[])row.Cells[1].Value;
    this.PictureBox.Image = Image.FromStream(new MemoryStream(picture));
 }