C# 从mysql数据库c中检索blob图片#
我使用这段代码来检索我的图片,它与一个只包含blob的简单表协同工作,但当我试图为我的表用户调整它时,它包含(cin、nom、prenom….、image)异常,表明 “参数无效”(不是有效参数) 试试这个C# 从mysql数据库c中检索blob图片#,c#,mysql,blob,C#,Mysql,Blob,我使用这段代码来检索我的图片,它与一个只包含blob的简单表协同工作,但当我试图为我的表用户调整它时,它包含(cin、nom、prenom….、image)异常,表明 “参数无效”(不是有效参数) 试试这个 DataTable userTable; DataTable ds; int cin; string nom; string prenom; Byte[] ImageByte; userTable = ds; if (userTable == null) return false
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;
}
这应该可以解决问题。在哪一行出现异常?我认为您不必填写数据集,而是直接使用SqlDataReader。如果我删除了捕获,我不会出错,也不会有结果……谢谢,但我只需要在PictureBexpress中获得一个blob图片,请检查我更新的答案。您需要将字节数组转换为位图。请检查ByteToImage(byte[]byteArray)方法您能否在代码周围添加一些解释,而不仅仅是发布答案?试着帮助人们准确地理解哪一部分是错误的,为什么是错误的,为什么你的解决方案是他们需要应用的。
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"]);