Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用codebehind动态更改图像高度_C#_Asp.net_Image Processing - Fatal编程技术网

C# 使用codebehind动态更改图像高度

C# 使用codebehind动态更改图像高度,c#,asp.net,image-processing,C#,Asp.net,Image Processing,我想使用代码隐藏重新调整asp:image的大小。我将根据图像的高度重新调整图像的大小,使其适合放置。到目前为止我有 Image img1 = ((Image)e.Item.FindControl("prodImage")); int finalWidth = Convert.ToInt32(img1.Width); int maxWidth = 20; if (finalWidth > maxWidth)

我想使用代码隐藏重新调整asp:image的大小。我将根据图像的高度重新调整图像的大小,使其适合放置。到目前为止我有

        Image img1 = ((Image)e.Item.FindControl("prodImage"));
        int finalWidth = Convert.ToInt32(img1.Width);
        int maxWidth = 20;

        if (finalWidth > maxWidth)
        {
            resize
        }
        else
        {
            dont resize
        }
我得到一个转换错误,因为img1.width是一个web单元。我试过了,但无法将int转换为int或将int转换为一个单位


感谢所有能够提供帮助的人

您可以使用此代码

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

从“”中看到这个答案

以下是您需要的相关代码:

    /// <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;
    }
//
///将图像调整为指定的宽度和高度。
/// 
///要调整大小的图像。
///要调整大小的宽度。
///要调整大小的高度。
///调整大小的图像。
公共静态System.Drawing.Bitmap ResizeImage(System.Drawing.Image图像,整型宽度,整型高度)
{
//结果的持有者
位图结果=新位图(宽度、高度);
//使用图形对象将调整大小的图像绘制到位图中
使用(Graphics=Graphics.FromImage(结果))
{
//将“调整大小质量”模式设置为“高质量”
graphics.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//将图像绘制到目标位图中
graphics.DrawImage(图像,0,0,结果.宽度,结果.高度);
}
//返回生成的位图
返回结果;
}

我找到了不用图形库就可以完成的方法

        //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;
    }
System.Drawing.Size Size=LockAspectRatio(sFullFilePathAndName,Convert.ToInt32(rdPhoto.Width.Value)-20,Convert.ToInt32(rdPhoto.Height.Value)-80);
imgRep.Width=新单位(尺寸宽度);
imgRep.Height=新单位(尺寸高度);
public System.Drawing.Size LockAspectRatio(字符串sFilePath,双最大宽度,双最大高度)
{
int newWidth,newHeight;
System.Drawing.Size Size=新的System.Drawing.Size();
System.Drawing.Image=System.Drawing.Image.FromFile(sFilePath);
size.Width=image.Width;
size.Height=image.Height;
newWidth=image.Width;
newHeight=image.Height;
双图像=(双)图像.宽度/(双)图像.高度;
//步骤1:如果图像大于容器,则缩小它
如果(image.Width>maxWidth)
{
size.Height=(int)数学圆(maxWidth*imgRatio,0);
size.Width=(int)maxWidth;
newWidth=大小。宽度;
newHeight=大小。高度;
}
如果(新高度>最大高度)
{
size.Height=(int)maxHeight;
size.Width=(int)数学圆(maxHeight*imgRatio,0);
}
//步骤2:如果图像小于容器,则拉伸它(可选)
if((image.Width最大高度)
{
size.Height=(int)maxHeight;
size.Width=(int)数学圆(maxHeight*imgRatio,0);
}
}
返回大小;
}

注意:如果您只需要一个小的缩略图,这非常好,但是如果您创建的是较大的缩略图,则图像质量将非常差。优点是它的性能很高。20像素对我来说看起来很小。我同意,我只是在给这个线程中发生的任何人做一个记录。你必须像这样取单位的值
int finalWidth=(int)img1.Width.Value请记住,使用上面的代码,您只是更改浏览器缩放图像的方式,而不是更改实际图像这看起来是可行的。我稍后会测试一下,然后再给你回复。ThanksI发现了如何在不使用图形库的情况下实现这一点