从SQL Server 2008中检索图像和asp.net的图像控制器displying它 [英] Retrieving images from SQL Server 2008 and displying it in a image controller of asp.net
问题描述
我在SQL Server 2008中的表图片
,它包含两列 imageid
和图片
。我能图像插入图片
使用文件上传
控制器和形象,我已经转换为二进制形式列存储。现在我想与图片ID对应的图像框来显示图像。我使用的是文本框具有用于显示执行的命令在 ID
用户类型和一个按钮。
I have a table images
in SQL Server 2008 and it contains two columns imageid
and image
. I am able to insert images into the image
column using the FileUpload
controller and image which I have converted to binary form for storing. Now I want to display the images in an image box corresponding with the image id. I am using a textbox for having the user type in the id
and a button to execute the command for displaying.
public void ShowImage()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["anu"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ID,Image from Images where ID=" + txtid.Text;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataReader dreader = cmd.ExecuteReader();
dreader.Read();
Context.Response.BinaryWrite((byte[])dreader["image"]);
dreader.Close();
con.Close();
}
这code为工作正常,但它加载在一个单独的页面图像,但我需要在一个特定的图像框来显示它。我的前端ASP.NET。如果有任何人知道答案,请帮助我。
This code is working fine but it loads the image in a separate page but I need to display it in a particular image box. My front end is ASP.NET. If any one knows the answer, please help me.
推荐答案
首先,你应该真正用在查询参数(或存储过程)
First you should really use a parameter in your query (or a stored procedure)
和你可以创建一个从二进制数据图像和保存到的OutputStream用正确的imageformat。
And you can just create an Image from the binary data and save that to the outputstream with the correct imageformat.
var ms = new MemoryStream((byte[])dreader["image"]);
var image = Image.FromStream(ms);
Context.Response.ContentType = "image/jpeg"; //or your actual imageformat
image.Save(Context.Response.OutputStream, format);
这篇关于从SQL Server 2008中检索图像和asp.net的图像控制器displying它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!