📄 imagehandler.ashx
字号:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public class ImageHandler : IHttpHandler
{
//取得数据库连接设置
static ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"];
public void ProcessRequest(HttpContext context)
{
MemoryStream ms=null;
try
{
//取得员工代号
string EmployeeID = context.Request.QueryString["EmployeeID"];
//通过ReadImage类的GetImage()方法取得SQL Server中图片资料
//创建Sql命令
string strSQL = "Select Photo from Employees where EmployeeID=@paramEmployeeID";
//创建SqlDataSource
SqlDataSource sqldsPhoto = new SqlDataSource(connString.ConnectionString, strSQL);
sqldsPhoto.SelectParameters.Add("paramEmployeeID", TypeCode.Int32, EmployeeID);
//通过SqlDataSource进行查询
DataView dv = (DataView)sqldsPhoto.Select(DataSourceSelectArguments.Empty);
//返回DataView第一个Row的Photo字段资料
Byte[] PhotoImage = (Byte[])dv[0]["Photo"];
ms = new MemoryStream(PhotoImage, 0, PhotoImage.Length);
}
catch
{
}
if (ms != null)
{
//取得图像MemoryStream大小
int bufferSize = (int)ms.Length;
//创建 buffer
byte[] buffer = new byte[bufferSize];
//调用MemoryStream.Read,自MemoryStream 读取至buffer,并返回count
int countSize = ms.Read(buffer, 0, bufferSize);
//返回图像buffer
context.Response.OutputStream.Write(buffer, 0, countSize);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -