从mysql数据库中检索blob图片c#

时间:2016-02-29 11:14:07

标签: c# mysql blob

我使用这段代码来检索我的图片,它使用一个只包含blob的简单表格,但当我正在尝试为我的表用户调整它时(cin,nom,prenom ....,image )表示

的异常
  

“Paramétre无效”(不是有效参数

        int bufferSize = 1000; 
        try
        {
            string SQL = "Select image from user ";

            MySqlCommand cmd = new MySqlCommand(SQL, db.Connection);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "image");
            int c = ds.Tables["image"].Rows.Count;
            db.CloseConnection();

            if (c > 0)
            {
                Byte[] byteBLOBData = new Byte[bufferSize];
                byteBLOBData = (Byte[])(ds.Tables["image"].Rows[c - 1]["image"]);
                MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);

                pictureBox1.Image = Image.FromStream(stmBLOBData);
                MessageBox.Show("bien chargée");
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Connection Error!\n" + ex.Message, "Error Message",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

2 个答案:

答案 0 :(得分:0)

试试这个...

DataTable userTable;
DataTable ds;

int cin;
string nom;
string prenom;
Byte[] ImageByte;

userTable = ds;
if (userTable == null)
    return false;
else
{
    if (userTable.Rows.Count > 0)
    {
        foreach (DataRow userRow in userTable.Rows)
        {
            cin = Convert.ToInt32(userRow["cin"]);
            nom = userRow["nom"].ToString();
            prenom = userRow["prenom"].ToString();
            ImageByte = (Byte[])(userRow["image"]);
        }
    }
}
if (ImageByte != null)
{
    // You need to convert it in bitmap to display the imgage
    pictureBox1.Image = ByteToImage(ImageByte);
    pictureBox1.Refresh();
}

public static Bitmap ByteToImage(byte[] blob)
{
    MemoryStream mStream = new MemoryStream();
    byte[] pData = blob;
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
    Bitmap bm = new Bitmap(mStream, false);
    mStream.Dispose();
    return bm;

}

答案 1 :(得分:0)

byteBLOBData = ((Byte[])ds.Tables["image"].Rows[c - 1]["image"]);

这应该解决它。