显示图像处理程序与调整大小

本文关键字:调整 图像处理 程序 显示 | 更新日期: 2023-09-27 18:14:00

在此之前,我在数据库中保存了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处理程序

显示图像处理程序与调整大小

显示之前重新调整二进制数据的大小

使用Image Resize lib http://imageresizing.net/。

您需要按比例调整图像大小并保持其长宽比。使用Image resize库,像这样的函数可以完成这项工作:

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);
}