Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将图像存储在db中,然后在picturebox中显示_C#_Image_Byte - Fatal编程技术网

C# 如何将图像存储在db中,然后在picturebox中显示

C# 如何将图像存储在db中,然后在picturebox中显示,c#,image,byte,C#,Image,Byte,在数据库中存储图像: 我有这个代码,它能够在数据库中存储图像的byte[]值, 首先,我将图像转换为字节,然后将其加载到列表并保存在数据库中 private byte[] GetPic(Image img) { using (MemoryStream ms = new MemoryStream()) { Bitmap bmp = new Bitmap(pboxInvent.Image); bmp.Save(ms, System.Drawing.Im

在数据库中存储图像: 我有这个代码,它能够在数据库中存储图像的byte[]值, 首先,我将图像转换为字节,然后将其加载到列表并保存在数据库中

private byte[] GetPic(Image img)
{
    using (MemoryStream ms = new MemoryStream())
    {
        Bitmap bmp = new Bitmap(pboxInvent.Image);
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        ms.Position = 0;
        byte[] data = new byte[ms.Length];
        ms.Read(data, 0, data.Length);
        return data.ToArray();
    }
}
public void fillProdList(int catID)
{
    ListCollection.listProduct.Clear();
    ProductInventory pI = new ProductInventory();
    pI.Image = imageToByteArray(pboxInvent.Image);
    ListCollection.listProduct.Add(pI);
}
但我的问题是长度只有13,当我试图拉出图像并将其加载到图片框时,错误表明它内存不足

下面是我将字节转换为图像并将其加载到图片框的过程:

public Image byteArrayToImage(int prodid)
{
    byte[] imagebyte = dbConnect.retrieveimagefromdb("select imgPhot from prod_Table where prodID=@ID", prodid);
    MemoryStream ms = new MemoryStream(imagebyte);
    ms.Seek(0, SeekOrigin.Begin);
    ms.Write(imagebyte, 0, imagebyte.Length);
    Image image = Image.FromStream(ms); //this line as error as well: parameters invalid
    return image;
} 

public byte[] retrieveimagefromdb(string cmdString, int prodid)
{
    SqlCommand cmd = new SqlCommand(cmdString, SqlConnect);
    cmd.Parameters.Add("@ID", SqlDbType.Int);
    cmd.Parameters["@ID"].Value = prodid;
    SqlConnect.Open();
    byte[] barrImg = (byte[])cmd.ExecuteScalar();
    return barrImg;
}

private void dgvProdList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    rbEdit.Enabled = true;
    rbAdd.Checked = false;
    disablingProdTextboxes();
    if(e.RowIndex >= 0)
    {
        DataGridViewRow row = this.dgvProdList.Rows[e.RowIndex];
        byte[] myimage = new byte[0];
        txtProdID.Text = row.Cells["prodID"].Value.ToString().Trim();
        txtProdName.Text = row.Cells["prodName"].Value.ToString().Trim();
        txtModel.Text = row.Cells["prodModel"].Value.ToString().Trim();
        tbSellPrice.Text = String.Format("{0:N2}",row.Cells["sellingPrice"].Value).Trim();
        tbunitPrice.Text = String.Format("{0:N2}", row.Cells["unitPrice"].Value).Trim();
        tbVatper.Text = String.Format("{0:N2}", row.Cells["vatPercentage"].Value).Trim();
        tbmuper.Text = String.Format("{0:N2}", row.Cells["markupPercentage"].Value).Trim();
        txtQty.Text = row.Cells["prodQty"].Value.ToString().Trim();
        txtDesc.Text = row.Cells["prodDesc"].Value.ToString().Trim();
        cbProdCategory.SelectedItem = row.Cells["catName"].Value.ToString().Trim();
        pboxInvent.Image = byteArrayToImage(int.Parse(row.Cells["ProdID"].Value.ToString().Trim()));
    }
}
更新::

如果我使用thsi代码,在缓冲区字节数组中插入这个,对不起,我有点不在这里

_sbScript.Append("0x");
for(int i=0;i}

您是否100%确定图像正确存储在数据库中<代码>图像.FromStream(ms)引发异常是流未保存有效映像的良好指示
ms.Seek(0,SeekOrigin.Begin);ms.Write(imagebyte,0,imagebyte.Length)这些是不必要的,因为您已经在constructor.ok中传递了数组。非常感谢。我在数据库中的数据类型是Image。如何确保流保存有效的图像?检索时,我的代码提取图像字节值{byte[13]},但保存在数据库中的字节看起来像0x53797374656D2E427974655B5D;选中此项: