使用asp.net中的Image Control将图像作为二进制数据插入数据库 [英] Insert Image to database as binary data using Image Control in asp.net
本文介绍了使用asp.net中的Image Control将图像作为二进制数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用图像控件将图像文件作为二进制数据上传到数据库。
i希望将Image1.ImageUrl中的文件上传到数据库
Hi,
I Want to Upload Image file to the database as binary data using image control.
i want to uplaod a file from Image1.ImageUrl to the Database
推荐答案
Hi,
public static void PerisitImage(string path, IDbConnection connection)
{
using (var command = connection.CreateCommand ())
{
Image img = Image.FromFile (path);
MemoryStream tmpStream = new MemoryStream();
img.Save (tmpStream, ImageFormat.Png); // change to other format
tmpStream.Seek (0, SeekOrigin.Begin);
byte[] imgBytes = new byte[MAX_IMG_SIZE];
tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "payload";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery ();
}
}
这篇关于使用asp.net中的Image Control将图像作为二进制数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文