本文介绍了图像存储和相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从mysql数据库提取图像.
我已将图像另存为Longblob,FileName和FileSize.
请帮助我.
how to reterive image from mysql database.
I have save image as Longblob,FileName and FileSize.
pls help me.
推荐答案
public byte[] GetImage(int id, string connectionString)
{
using (var conn = new MySqlConnection(connectionString))
{
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT Image FROM MyTable WHERE id = ?id";
cmd.Parameters.Add("?id", id);
using (var reader = cmd.ExecuteReader())
{
if (!reader.Read())
{
return null;
}
byte[] buffer = new byte[2048];
long bytesRead;
long fieldOffset = 0;
using (var stream = new MemoryStream())
{
while ((bytesRead = reader.GetBytes(reader[0], fieldOffset, buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, (int)bytesRead);
fieldOffset += bytesRead;
}
return stream.ToArray();
}
}
}
}
}
这篇关于图像存储和相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!