从 MySQL 数据库 C# 中检索 blob 图片
本文关键字:检索 blob 图片 MySQL 数据库 | 更新日期: 2023-09-27 18:36:38
我使用此代码来检索我的图片,它与仅包含 blob 的简单表一起工作,但是当我尝试为我的表用户调整它时,包含 (cin,nom,prenom....,图像) 异常指示
"参数无效"(不是有效参数)
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;
}
byteBLOBData = ((Byte[])ds.Tables["image"].Rows[c - 1]["image"]);
这应该可以解决它。