自己写的比例缩放图片方法,show 一下!

来源:互联网 发布:linux到底有什么用 编辑:程序博客网 时间:2024/05/19 00:38

/// <summary>
    /// 显示图片
    /// </summary>
    /// <param name="image"></param>
    void ShowImage(string image, int max)
    {
            string str = Server.MapPath("~/productimages/" + image);
            System.Drawing.Bitmap bit = new Bitmap(str);
            double bili = Convert.ToDouble(bit.Height) / Convert.ToDouble(bit.Width);
            if (bit.Width > bit.Height)
            {
                if (bit.Width < max)
                {
                    this.Image1.Width = bit.Width;
                    this.Image1.Height = bit.Height;
                }
                else
                {
                    int height = Convert.ToInt32((bili * max));

                    this.Image1.Width = max;
                    this.Image1.Height = height;
                }
            }
            else
            {
                if (bit.Height < max)
                {
                    this.Image1.Width = bit.Width;
                    this.Image1.Height = bit.Height;
                }
                else
                {
                    int width = Convert.ToInt32(((1 / bili) * max));

                    this.Image1.Width = width;
                    this.Image1.Height = max;
                }
            }

            Image1.ImageUrl = "~/productimages/" + image;

       
    }