我正在尝试在通用处理程序中调整图像服务器端的大小,然后将其从数据库中的 BLOB 转换回图像...这是我的处理程序代码:
<%@ WebHandler Language="C#" Class="Image" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public class Image : IHttpHandler {
public void ProcessRequest (HttpContext context) {
Guid id = new Guid(context.Request.QueryString["Id"]);
int column = 7;
if (context.Request.QueryString["img"] == "tbn")
{
column = 6;
}
context.Response.ContentType = "image/png";
MemoryStream strm = new MemoryStream(returnImage(id, column));
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
public Byte[] returnImage(Guid id, int column)
{
SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=password");
string qry = "SELECT * FROM Project WHERE Id=@id";
SqlCommand cmd = new SqlCommand(qry, sqlCn);
cmd.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = id;
sqlCn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Byte[] ar = (Byte[])(dr[column]);
dr.Close();
cmd.Dispose();
sqlCn.Close();
return ar;
}
public bool IsReusable {
get {
return false;
}
}
}
它应该看起来图像宽度是否高于其高度,反之亦然,并根据该设置设置高度或宽度,并且应按比例设置其他值(高度/宽度),以免被拉伸。
我发现的是: http: //www.codeproject.com/Articles/25838/A-Simple-Image-Handler
但我真的不知道如何使用它......有什么建议吗?感谢您提前提供的所有帮助!