从sqlServer的image字段获取并显示图像示例(ASPNET2.0)

本文介绍了两种从SQLServer的image字段获取并显示图像的方法。第一种方法涉及两个页面,通过Response.BinaryWrite将image类型图片显示在页面上。第二种方法使用HTTPHandler处理程序,从数据库中读取照片数据并直接输出到浏览器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方法:
要两个页,第一个页面放Image控件(A.aspx);另一个页面用 Response.BinaryWrite((byte[])users.PhotoData)显示image类型图片(B.aspx);

A.aspx:

<asp:Image ID="ManagerPhoto" runat="server" Width="50px" />

A.aspx.cs:

  ManagerPhoto.ImageUrl = "B.aspx?ID=" + ID.ToString();

B.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
            Users users = _org.GetUsersInfomationByID(Convert.ToInt32(Request.QueryString["ID"]));
            Response.BinaryWrite((byte[])users.PhotoData);
        }
第二种方法:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Configuration;
public class Handler : IHttpHandler   {
   
    public bool IsReusable   {
        get   {
            return true;
        }
    }
    public void ProcessRequest(HttpContext context)   {
        // Set up the response settings
        context.Response.ContentType = "image/jpeg";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput = false;
        Stream stream = null;
        if (context.Request.QueryString["employeeID"] != "")   {
            stream = GetPhoto(context.Request.QueryString["employeeID"]);
        }
        const int buffersize = 1024 * 16;
        byte[] buffer = new byte[buffersize];
        int count = stream.Read(buffer, 0, buffersize);
        while (count > 0)   {
            context.Response.OutputStream.Write(buffer, 0, count);
            count = stream.Read(buffer, 0, buffersize);
        }
    }
    public Stream GetPhoto(string photoId)   {
        SqlConnection myConnection = new SqlConnection("server=192.168.3.17;database=mypeople;user id='sa';password='");
        SqlCommand myCommand = new SqlCommand
            ("SELECT [照片] FROM [people] WHERE [职工编号]=@PhotoID",
            myConnection);
        myCommand.CommandType = CommandType.Text;
        myCommand.Parameters.Add(new SqlParameter("@PhotoID", photoId));
        myConnection.Open();
        object result = myCommand.ExecuteScalar();
       
        try   {
            return new MemoryStream((byte[])result);
        }
        catch (ArgumentNullException e)   {
            return null;
        }
        finally   {
            myConnection.Close();
        }
    }
}
/* ----------------------引用方法---------------------------------------
   <asp:Image ID="imgPhoto" ForeColor =Red runat="server"
        ImageUrl='<%# "Handler2.ashx?employeeID=" + Eval("职工编号") %>'
        AlternateText="该员工无照片" Height="248px" Width="188px" />
 -----------------------------------------------------------------------*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值