如何用Response对象输出图像

来源:百度知道 编辑:UC知道 时间:2024/04/28 01:18:59
刚刚学.net 数据库也没有学。 所以碰到了一个问题 想请教一下
函数Page_Load(object sender,EventArgs e) 从数据库ShoppingDB的图片Pictures中获取一个ID值为1的图片,然后使用byte数组Date保存图片的数据。
下面的代码是事件。
我用的是access数据库,请问如何设计数据库,才能利用下面的代码在网页中输出图片呢。具体的步骤是什么呢,最好能详细点,刚学,什么也不太懂,谢谢。我用的是VS 2005。

protected void Page_Load(object sender,EventArgs e)
{
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
string cmdText = "SELECT * FROM PictureTab WHERE PictureID='1'";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataReader dr = null;
try
{
myConnection.Open();
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
throw new Exception(ex.Message,ex);
}
byte[] Data = null;

while(dr.Read())
{
Data = (byte[])dr["Data"];
Response.ContentTy

其实最好保存路径,然后在img的src中填上

比如在ashx文件里:
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "image/gif";
context.Response.WriteFile(strImagePath);
}

或者
context.Response.ContentType = "image/gif";
context.Response.Clear();
context.Response.BufferOutput = true;
graphics.Save(context.Response.OutputStream, ImageFormat.Jpeg);