0

我有一个数据列表控件,通过它我通过图像处理程序从数据库中检索图像。

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

}

4

1 回答 1

0

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

于 2013-04-19T15:23:09.577 回答