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

标签 c# mysql blob

我使用此代码来检索我的图片,它与一个仅包含 blob 的简单表一起工作,但是当我尝试将其调整为包含 (cin,nom,prenom....,image ) 异常表示

"Paramétre non valid" (not a valid parameter)

        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);
        }

最佳答案

试试这个...

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;

}

关于c# - 从mysql数据库中检索blob图片c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698114/

相关文章:

php - 限制偏移量大于实际记录的mysql select语句

c++ - 如何证明 findContours 做的事情与 cvBlob 做的一样

c# - 如何通过 Office Automation 查找和关闭打开的 Office 文档

c# - "this is ClassName"语句在 C# 中意味着什么?

php - 实时/快速 mysql 查询生成器/查看器

c# - 使用 C# 读取附加到 HTTP 扩展的 BLOB

postgresql - lo_export 在 PostgreSQL 9.3 中不存在

c# - IEnumenator 出问题了

c# - 如何在两个 MVVM 对之间进行通信?

mysql - 如何在输入时从数据库获取结果并匹配所有数据?