gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:21:40 25 4
gpt4 key购买 nike

我使用此代码来检索我的图片,它与一个仅包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com