如何在asp.net的图像控件中显示byte []图像 [英] how to show byte[] image in image control in asp.net
本文介绍了如何在asp.net的图像控件中显示byte []图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示SQL 2008数据库中的二进制映像.
我想获取此图像并在ASP.NET的图像控件中显示.
谢谢.
I would like to show the binary image which is in SQL 2008 database.
I would like to fetch this image and show in image control in ASP.NET.
Thank you.
推荐答案
您要么需要将该Byte []存储在文件中,要么使用响应对象
1.使用System.IO.file.WriteAllByte(Byte []),然后将其提供给图片控件
2.
Either you need to store that Byte[] in file or use response object
1. Use System.IO.file.WriteAllByte(Byte[]) and then give that to picture control
2.
Response.ContentType = "image/jpg";
Response.BinaryWrite((Byte[])objReader["image"]);
您将必须创建一个名为Picture.aspx
的新页面.
you will have to create a new page called Picture.aspx
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int ImageID = DropDownList1.SelectedIndex;
Image1.ImageURL = "Picture.aspx?ImageID=" + ImageID;
}
在picture.aspx页面加载中
in picture.aspx pageload
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ImageID"] != null)
{
int ImageID = Convert.ToInt32( Request.Params["ImageID"]);
using(SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Inetpub\\wwwroot\\WebserviceUpload2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True"))
{
const string SQL = "SELECT [MIMEType], [Image] FROM [PictureTable] WHERE [ImageID] = @ImageID";
SqlCommand myCommand = new SqlCommand(SQL, myConnection);myCommand.Parameters.AddWithValue("@ImageID", ImageID);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(); if (myReader.Read())
{
Response.ContentType = myReader["MIMEType"].ToString();
Response.BinaryWrite((Byte[])myReader["image"]);
//Response.Pics(
}
myReader.Close();
myConnection.Close();
}
}
}
标记为答案可以解决您的问题,:)
mark as answer is solves your problem, :)
首先,您必须从数据库中检索图像的Byte [],
然后创建一个文件,将Byte []写入该文件(.jpg),并将该文件地址URI赋予Image标记.
作为示例代码
First You Have To Retrive Byte[] of a image from database,
And Create A file,write that Byte[] in to that file (.jpg) and Give that file address URI to Image tag.
As sample code
SqlConnection c = new SqlConnection("Your SQL Connection String")
SqlCommand cmd = new SqlCommand();
cmd.Connection = c;
cmd.CommandText = "Your Select Statement here to bring image"
try
{
c.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] Uimage = (byte[])dr[0];
// Differentiating images With the Session Id's
UserImageTempPath = @"\Path" + SessionId + ".jpg";
// To get Actual path of the Image folder
string path = Server.MapPath("") + UserImageTempPath;
FileStream fs = File.Create(path);
if (fs.Length != 0)
{
File.Delete(path);
fs = File.Create(path);
}
fs.Write(Uimage, 0, Uimage.Length);
fs.Close();
}
}
catch { }
finally { c.Close(); }
}
HtmlImage i1 = (HtmlImage)this.FindControl("image1");
HtmlImage i2 = (HtmlImage)this.FindControl("image2");
i1.Attributes.Add("src", UserImageTempPath);
i2.Attributes.Add("src", UserImageTempPath);
这篇关于如何在asp.net的图像控件中显示byte []图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文