使用代码隐藏动态更改图像高度 [英] Dynamically change an images height using codebehind

查看:141
本文介绍了使用代码隐藏动态更改图像高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用后面的代码重新调整asp:image的大小。我将根据它的高度重新调整图像的大小,以便它可以适应到位。到目前为止我已经

I want to re-size an asp:image using code behind. I'm gonna re-size the image depending on its height so it can fit into place. So far i have

        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是一个网络单元。我试过但不能把它作为一个int或者把int作为一个单元投射。

I am getting a conversion error because img1.width is a web unit. I tried but couldnt cast as an int or cast the int as a unit.

感谢任何可以提供帮助的人

Thanks to anyone who can help

推荐答案

请参阅高质量图像缩放C#

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

Here's the relevant bit of code that you'll need:

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

这篇关于使用代码隐藏动态更改图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆