本文介绍了[C#]如何从picturebox中的mysql中检索LONGBLOB图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我需要从mysql中检索图片的长片。
首先我将它插入数据库中:
Hello,
I need to retrieve the longblob of a picture from mysql.
So first I insert it in the database with this:
MemoryStream ms = new MemoryStream();
// PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
byte[] img = ms.ToArray();
string q = "insert into tb_produits (Image,Designation,Reference,CodeEAN,ChaineEAN,Quantite,PrixReviens,PrixVente) values('" + img+ "')";
这是检索图片:
Here is to retrieve the picture :
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;
}
MySqlDataAdapter sda = new MySqlDataAdapter("SELECT * FROM tb_produits WHERE Designation='" + Designation + "'and Reference='" + Reference + "'", MyConnexion);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
byte[] img = (byte[])dt.Rows[0]["Image"];
PIC_Image.Image = ByteToImage(img);
}
我的尝试:
我试图检索但是我收到了一个错误:
参数在这一行无效Bitmap bm = new Bitmap(mStream,false);
What I have tried:
I tried to retrieve but I got an error:
Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
推荐答案
byte[] img = (byte[])dt.Rows[0]["Image"];
在这里你应该尝试这样:
Here you should try put like this:
byte[] img = (byte[])dt.Rows[0]["put the number of column start from zero, in this case I'll say: " 1]
我认为这应该解决它。
I think this should fix it.
这篇关于[C#]如何从picturebox中的mysql中检索LONGBLOB图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!