0
我想调整我一般的处理程序中的图像服务器侧将其转换回从数据库中的BLOB的图像之后...这是我处理代码:如何在从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 [email protected]";
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;
}
}
}
看起来应该wheter图像宽度高于其高度,相反,根据设置的高度或宽度和其他值(高度/宽度)应按比例设置,因此它不会被拉伸。
我发现这是什么:http://www.codeproject.com/Articles/25838/A-Simple-Image-Handler
但我真的不知道如何使用它...有什么建议?感谢所有提前的帮助!
嗯...这个代码是巨大的:)和非常混乱......我怎么会用这个跟我的代码? – webster69
代码可以变得更紧凑,我会授予你 - 我只是从一个旧项目中抓取它,我没有时间为你重写它。你可以通过调用第一个函数,将它传递给你的内存流,以及想要调整图像大小的宽度和高度,并将结果放回到内存流中来使用它。我会添加一个例子。 – JcFx