图像处理程序:添加高度方面?

时间:2013-04-19 15:02:30

标签: c# asp.net image image-processing

我有一个datalist cotrol,通过它可以通过图像处理程序从数据库中检索图像。

<asp:DataList ID="dlImages" runat="server" RepeatColumns="2"RepeatDirection="Horizontal" BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px"  CssClass="DataList-Centre" DataSourceID="CustomerDS" Width="61%" style="text-align: center">
    <ItemTemplate>
        <div style="width:300px; height:300px;">
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ATT_name") %>' Font-Bold="True" Font-Size="10pt" ForeColor="#336699" Width="100%" />
            <br />
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/ImageHandler.ashx?MaxWidth=490&AttachmentId=" +   Eval("CustomerID") %>'/>
        </div>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width ="100px"  />
</asp:DataList>

我的imagehandler.ashx代码如下所示。一切都很好但我无法调整图像的高度而不会使像素比率搞砸。如何为我的处理程序添加高度方面以获取下面的代码?

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int attachmentId = -1;
        int maxWidth = 2000;
        int rotate = 0;
        if(HttpContext.Current.Request.QueryString["AttachmentId"] != null)
            attachmentId = Convert.ToInt32(HttpContext.Current.Request.QueryString["AttachmentId"]);
        if(HttpContext.Current.Request.QueryString["MaxWidth"] != null)
            maxWidth = Convert.ToInt32(HttpContext.Current.Request.QueryString["MaxWidth"]);
        if(HttpContext.Current.Request.QueryString["Rotate"] != null)
         rotate =              Convert.ToInt32(HttpContext.Current.Request.QueryString["Rotate"]);

        HttpResponse r = context.Response;

        if (attachmentId != -1)
        {
            using (SqlConnection cn = new SqlConnection(
                ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(
                    string.Format("SELECT Rel_ObjectId, Content_Type, Att_Name, Data_Size, Data"
                        + " FROM Customer"
                        + " WHERE AttachmentId = '{0}'"
                        , attachmentId )
                    , cn);

                cn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {                            
                        r.ContentType = reader["Content_Type"].ToString();

                        using(MemoryStream ms_in = new MemoryStream((byte[])reader["Data"]))
                        {
                            Image img_in = Image.FromStream(ms_in);
                            const int orientationId = 0x112;
                            int orientationIndex = Array.IndexOf(img_in.PropertyIdList, orientationId);
                            if(orientationIndex > -1)
                            {
                                int orientation = img_in.GetPropertyItem(orientationId).Value[0];
                                switch (orientation)
                                {
                                    case 3: rotate += 180;
                                            break;
                                    case 6: rotate += 90;
                                            break;
                                    case 8: rotate += 270;
                                            break;
                                }
                            }
                            if(rotate < 0)
                                rotate += 360;
                            rotate = rotate % 360;
                            //If need to rotate
                            switch(rotate)
                            {
                                case 90:    img_in.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                            break;
                                case 180:   img_in.RotateFlip(RotateFlipType.Rotate180FlipNone);
                                            break;
                                case 270:   img_in.RotateFlip(RotateFlipType.Rotate270FlipNone);
                                            break;
                            }

                            // If need to shrink image, then do so
                            double reductionRatio = (double)maxWidth / img_in.Width;
                            Image img_out;
                            if(maxWidth<img_in.Width)
                                img_out = img_in.GetThumbnailImage(maxWidth, (int)(img_in.Height*reductionRatio), new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                            else
                                img_out = img_in;

                            // Write img_out
                            MemoryStream ms_out = new MemoryStream();
                            img_out.Save(ms_out, System.Drawing.Imaging.ImageFormat.Jpeg);
                            byte[] data_out = new byte[ms_out.Length];
                            ms_out.Position = 0;
                            ms_out.Read(data_out, 0, (int)ms_out.Length);
                            r.BinaryWrite(data_out);

                            // Clean up
                            img_in.Dispose();
                            img_out.Dispose();
                            ms_out.Dispose();
                        }
                    }
                }
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public bool ThumbnailCallback()
    {
        return true;
    }
}

}

1 个答案:

答案 0 :(得分:0)

我认为缩小比例应该是img_in.Width / maxWidth而不是maxWidth / img_in.Width。因为您将新高度定义为高度* reductionRatio。