使用resize显示图像处理程序

时间:2014-07-22 11:38:29

标签: c# asp.net ashx

之前,我在数据库,实际大小和缩略图中保存了2个大小的图像,然后在需要时显示每个图像。

在ashx处理程序中我设置了我需要的类型,这是我的代码:

            string field = context.Request.QueryString["field"];
            string table = context.Request.QueryString["table"];
            string id = context.Request.QueryString["id"];

            string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
            SqlConnection myConnection = new SqlConnection(conn);
            myConnection.Open();

            string sql = "";
            sql = "Select " + field + ", pictureType from " + table + " where id=@imageId";
            SqlCommand cmd = new SqlCommand(sql, myConnection);

            cmd.Parameters.Add("@imageId", SqlDbType.Int).Value = id;
            cmd.Prepare();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();

            context.Response.ContentType = dr["pictureType"].ToString();
            context.Response.BinaryWrite((byte[])dr[field]);
            dr.Close();
            myConnection.Close();

我用这种方式:

<img src="handlers/ShowPic.ashx?table=tblEnBackGrounds&field=image&id=1" alt="s" />

但现在我决定只保存实际尺寸的图像,然后在ashx文件中重新调整大小并显示正确的类型(这里是真实或缩略图)。

现在我需要先到现在是好还是不好?第二,我不知道如何在ashx处理程序中显示之前重新调整二进制数据的大小

1 个答案:

答案 0 :(得分:0)

使用精美的Image Resize lib http://imageresizing.net/

您需要按比例调整图像大小并保持其宽高比。使用Image resize lib,这样的事情可能是完成这项工作的功能:

public static byte[] resizeProportionaly(byte[] imageFile, int pWidth, int pHeight, string crop = "&crop=auto")
    {
        MemoryStream stream = new MemoryStream(imageFile);
        stream.Seek(0, SeekOrigin.Begin);
        ResizeSettings resizeCropSettings = new ResizeSettings(string.Format("bgcolor=FFFFFF&width={0}&height={1}&format=jpg" + crop, pWidth, pHeight));
        Image originalImage = Image.FromStream(stream, true, false);


        Image resizedImage = ImageBuilder.Current.Build(originalImage, resizeCropSettings);
        MemoryStream ms = new MemoryStream();
        resizedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

并在你的处理程序中这样:

public void ProcessRequest (HttpContext context) {
    // some init code...take care of your query strings here etc.

    string cropString = "x1,y1,x2,y2"; // if you need croping? 
    // change height and width based on your picture type
    int outWidth = 640; // whatever you need here
    int outHeight = 480;

    System.Data.DataRow data = GetDBPhotoRecord(photoId);
    byte[] photoStream = (byte[])data[mxmPhotos.Photo]; 
    // if you dont need cropping just remove the last cropString parameter
    byte[] outStream = resizeProportionaly(photoStream, outWidth, outHeight, cropString);

    // ...

    context.Response.Buffer = true;
    context.Response.Clear();
    context.Response.ContentType = "image/jpeg";
    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(7));
    context.Response.BinaryWrite(outStream);
}