如何使用C#在ASP.NET中检索数据库二进制图像 [英] How to retrieve binary image from database using C# in ASP.NET
问题描述
我需要从数据库中检索的二进制图像。
我的查询如下所示。
的SqlConnection CON =新的SqlConnection(@数据源=本地主机;初始目录= MyGames;集成安全性=真);
CMD的SqlCommand =新的SqlCommand(从CorrespondingBall选择blueBallImage WHERE目标=默认球,CON);
我不知道如何检索blueBallImage这是一个二进制图像。
在我已经成功找回它,我需要使用含有文本一个DropDownList文字添加到图像。在codeS如下所示。
BMP位图=新位图(@C:\\用户\\ apr13mpsip \\文档\\ Visual Studio 2012中\\网站已\\ CorrespondingBallWebSite \\图片\\ blueBallDefault.png);
暂时,我不知道如何检索图像。因此,我很难codeD它,我不想要的。我想从数据库中检索。
图形GRA = Graphics.FromImage(BMP);gra.DrawString(ddlCharacter.Text,新的字体(宋体,18),Brushes.Black,新的PointF(4,6));MemoryStream的MS1 =新的MemoryStream();
bmp.Save(MS1,ImageFormat.Png);
变种base64Data = Convert.ToBase64String(ms1.ToArray());
imgImage.ImageUrl =数据:图像/ PNG; BASE64,+ base64Data;
下面是一个基本的样品快速加载从数据库中的图像,并加载到ASP一个HTML图像源。请告诉我,如果你的作品; - )
//从图像文件与基本的查询数据库中的字节数组
SqlDataAdapter的myAdapter1 =新SqlDataAdapter的(选择[标志] FROM [DBO] [tblCompanyInfo]GlobalUser.currentConnectionString);
DataTable的DT =新的DataTable();
myAdapter1.Fill(DT);的foreach(在dt.Rows的DataRow行)
{
//从图像文件的字节数组
字节[] = imgBytes(字节[])行[标识]; //如果你想转换为位图文件
TypeConverter的TC = TypeDescriptor.GetConverter(typeof运算(位图));
位图MYBITMAP =(位图)tc.ConvertFrom(imgBytes); 字符串imgString = Convert.ToBase64String(imgBytes);
//设置数据源:图像/ BMP
imgLogoCompany.Src =的String.Format(数据:图像/ BMP; BASE64,{0} \\,imgString);
}
I need to retrieve a binary image from the database.
My queries are as below.
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyGames;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select blueBallImage from CorrespondingBall WHERE objective = Default Ball", con);
I do not know how to retrieve blueBallImage which is a binary image.
After I have retrieve it successfully, I need to add a text onto the image using a dropdownlist which contain the text. The codes are as below.
Bitmap bmp = new Bitmap(@"C:\Users\apr13mpsip\Documents\Visual Studio 2012\WebSites\CorrespondingBallWebSite\Images\blueBallDefault.png");
For the time being, I do not know how to retrieve the image. Therefore, I hard coded it which I do not want. I want to retrieve it from database.
Graphics gra = Graphics.FromImage(bmp);
gra.DrawString(ddlCharacter.Text, new Font("Verdana", 18), Brushes.Black, new PointF(4, 6));
MemoryStream ms1 = new MemoryStream();
bmp.Save(ms1, ImageFormat.Png);
var base64Data = Convert.ToBase64String(ms1.ToArray());
imgImage.ImageUrl = "data:image/png;base64," + base64Data;
Here is a basic sample to load an image from database quickly and load into a html image source in ASP. Please tell me if it works for you ;-)
//Get byte array from image file in the database with basic query
SqlDataAdapter myAdapter1 = new SqlDataAdapter("Select [logo] FROM [dbo].[tblCompanyInfo]", GlobalUser.currentConnectionString);
DataTable dt = new DataTable();
myAdapter1.Fill(dt);
foreach (DataRow row in dt.Rows)
{
// Get the byte array from image file
byte[] imgBytes = (byte[]) row["logo"];
// If you want convert to a bitmap file
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap MyBitmap = (Bitmap)tc.ConvertFrom(imgBytes);
string imgString = Convert.ToBase64String(imgBytes);
//Set the source with data:image/bmp
imgLogoCompany.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString);
}
这篇关于如何使用C#在ASP.NET中检索数据库二进制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!