【问题标题】:ASP.Net MVC Image Upload Resizing by downscaling or paddingASP.Net MVC 图像上传通过缩小或填充调整大小
【发布时间】:2010-11-03 16:15:14
【问题描述】:

用户将能够上传图片。如果图像大于设定尺寸,我想将其缩小到该尺寸。显然,由于比率,它不必完全匹配,宽度将是关键大小,因此高度是可变的。

如果图像小于设置大小,我想创建一个设置大小的新图像,背景为定义颜色,然后将上传的图像居中放置,因此结果是带有填充颜色的原始图像。

非常感谢任何代码示例或链接

【问题讨论】:

  • 只需一行代码with this lib。如果图像更大,这会将图像缩小到指定的宽度,如果图像更小,则会简单地扩展画布。 ImageBuilder.Current.Build(postedFile, destination, new ResizeSettings("width=500&scale=upscalecanvas&bgcolor=green"));Here is a live example.

标签: c# asp.net asp.net-mvc image


【解决方案1】:

您可以将文件加载到位图对象中:

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

然后只需检查对象的宽度。对于问题的第二部分,我建议使用 ImageMagick 之类的工具

http://www.imagemagick.org/script/index.php

准确调整第一张图像的大小或创建背景图像并将两个图像合并在一起。

【讨论】:

    【解决方案2】:

    这是我快速敲出的一段代码,用于根据宽度调整它的大小。我相信您可以弄清楚如何为位图添加背景颜色。这不是完整的代码,而只是关于如何做事的想法。

    public static void ResizeLogo(string originalFilename, string resizeFilename)
    {
        Image imgOriginal = Image.FromFile(originalFilename);
    
        //pass in whatever value you want for the width (180)
        Image imgActual = ScaleBySize(imgOriginal, 180);
        imgActual.Save(resizeFilename);
        imgActual.Dispose();
    }
    
    public static Image ScaleBySize(Image imgPhoto, int size)
    {
        int logoSize = size;
    
        float sourceWidth = imgPhoto.Width;
        float sourceHeight = imgPhoto.Height;
        float destHeight = 0;
        float destWidth = 0;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;
    
        // Resize Image to have the height = logoSize/2 or width = logoSize.
        // Height is greater than width, set Height = logoSize and resize width accordingly
        if (sourceWidth > (2 * sourceHeight))
        {
            destWidth = logoSize;
            destHeight = (float)(sourceHeight * logoSize / sourceWidth);
        }
        else
        {
            int h = logoSize / 2;
            destHeight = h;
            destWidth = (float)(sourceWidth * h / sourceHeight);
        }
        // Width is greater than height, set Width = logoSize and resize height accordingly
    
        Bitmap bmPhoto = new Bitmap((int)destWidth, (int)destHeight, 
                                    PixelFormat.Format32bppPArgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
    
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, (int)destWidth, (int)destHeight),
            new Rectangle(sourceX, sourceY, (int)sourceWidth, (int)sourceHeight),
            GraphicsUnit.Pixel);
    
        grPhoto.Dispose();
    
        return bmPhoto;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 2016-05-03
    • 1970-01-01
    • 2020-06-16
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    相关资源
    最近更新 更多