使用隐藏代码动态更改图像高度

本文关键字:图像 高度 动态 隐藏 代码 | 更新日期: 2023-09-27 18:06:33

我想使用后面的代码重新调整asp:图像的大小。我要根据它的高度重新调整图像的大小,这样它就能放进去了。到目前为止,我有

        Image img1 = ((Image)e.Item.FindControl("prodImage"));
        int finalWidth = Convert.ToInt32(img1.Width);
        int maxWidth = 20;
        if (finalWidth > maxWidth)
        {
            resize
        }
        else
        {
            dont resize
        }

我得到一个转换错误,因为imag1。宽度是一个网页单位。我试过了,但是不能转换为整型,也不能将整型转换为单位。

感谢所有能帮助我的人

使用隐藏代码动态更改图像高度

请参阅"高质量图像缩放c# "中的答案。

这是你需要的相关代码:

    /// <summary>
    /// Resize the image to the specified width and height.
    /// </summary>
    /// <param name="image">The image to resize.</param>
    /// <param name="width">The width to resize to.</param>
    /// <param name="height">The height to resize to.</param>
    /// <returns>The resized image.</returns>
    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
    {
        //a holder for the result
        Bitmap result = new Bitmap(width, height);
        //use a graphics object to draw the resized image into the bitmap
        using (Graphics graphics = Graphics.FromImage(result))
        {
            //set the resize quality modes to high quality
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //draw the image into the target bitmap
            graphics.DrawImage(image, 0, 0, result.Width, result.Height);
        }
        //return the resulting bitmap
        return result;
    }

你可以使用下面的代码

double ratio = maxWidth / finalWidth;
int maxHeight = (int) img1.Height * ratio;
System.Drawing.Image resized= img1.GetThumbnailImage(maxWidth , maxHeight , null, IntPtr.Zero); 

看一下这个函数

我发现了如何在不使用图形库的情况下做到这一点

        //Create a new image and set it to the image that was loaded in initially.
        System.Web.UI.WebControls.Image image = loaded image;
        //Check if the image has a picture and if not put in the no_picture image.
        if (product.Image1.ToString() == "")
        {
            ((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl                = "no_picture.gif";
        }
        else
        {
            ((System.Web.UI.WebControls.Image)e.Item.FindControl("prodImage")).ImageUrl = "upload" + product.Image1.ToString();                
        }   
        //Call a function to keep the images within the borders of each repeater cell. Returns an Image object so we can access the new height/width.
        System.Web.UI.WebControls.Image dummyImg = keepAspectRatio(image, 110, 100);
        image.Width = new Unit(dummyImg.Width.Value);
        image.Height = new Unit(dummyImg.Height.Value); 
        public System.Web.UI.WebControls.Image keepAspectRatio(System.Web.UI.WebControls.Image image, double maxWidth, double maxHeight)
        {
           //Create an Image object to store width and height
           System.Web.UI.WebControls.Image dummyImage2 = new System.Web.UI.WebControls.Image();           
           //Ratio variables to calculate aspect
           double MaxRatio = maxWidth /  maxHeight;
           double ImgRatio = image.Width.Value /  image.Height.Value;
           //Compare the images width that was passed in to the max width allowed
           if (image.Width.Value > maxWidth)
           {
              //Set the dummy images height and width
              dummyImage2.Height = (int) Math.Round(maxWidth / ImgRatio, 0);
              dummyImage2.Width = (int)maxWidth;            
           }
           //Compare the images height that was passed in to the max height allowed
           if (image.Height.Value > maxHeight)
           {
              //Set the dummy images height and width
              dummyImage2.Height = (int)Math.Round(maxWidth * ImgRatio, 0);
              dummyImage2.Width = (int)maxHeight;            
            }
           //Returnthe dummy object so its parameters can be accessed
           return dummyImage2;
        }

您可以使用这个函数来保持比率:

                System.Drawing.Size size = LockAspectRatio(sFullFilePathAndName, Convert.ToInt32(rdPhoto.Width.Value) - 20, Convert.ToInt32(rdPhoto.Height.Value) - 80);
                imgRep.Width = new Unit(size.Width);
                imgRep.Height = new Unit(size.Height); 

public System.Drawing.Size LockAspectRatio(string sFilePath, double maxWidth, double maxHeight)
    {
        int newWidth, newHeight;
        System.Drawing.Size size = new System.Drawing.Size();
        System.Drawing.Image image = System.Drawing.Image.FromFile(sFilePath);
        size.Width = image.Width;
        size.Height = image.Height;
        newWidth = image.Width;
        newHeight = image.Height;
        double imgRatio = (double)image.Width / (double)image.Height;
        //Step 1: If image is bigger than container, shrink it
        if (image.Width > maxWidth)
        {
            size.Height = (int)Math.Round(maxWidth * imgRatio, 0);
            size.Width = (int)maxWidth;
            newWidth = size.Width;
            newHeight = size.Height;
        }
        if (newHeight > maxHeight)
        {
            size.Height = (int)maxHeight;
            size.Width = (int)Math.Round(maxHeight * imgRatio, 0);  
        }
        //Step 2: If image is smaller than container, stretch it (optional)
        if ((image.Width < maxWidth) && (image.Height < maxHeight))
        {
            if (image.Width < maxWidth)
            {
                size.Height = (int)Math.Round(maxWidth * imgRatio, 0);
                size.Width = (int)maxWidth;
                newWidth = size.Width;
                newHeight = size.Height;
            }
            if (newHeight > maxHeight)
            {
                size.Height = (int)maxHeight;
                size.Width = (int)Math.Round(maxHeight * imgRatio, 0);
            }
        }
        return size;
    }