ASP.Net MVC图片上传通过缩减或填充大小调整 [英] ASP.Net MVC Image Upload Resizing by downscaling or padding

查看:236
本文介绍了ASP.Net MVC图片上传通过缩减或填充大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用户可以上传图片。如果图像比一套尺寸更大的我想它缩小到的大小。显然它不必完全匹配因比,宽度将是关键尺寸,以便高度将是可变的。

A user will be able to upload an image. If the image is greater than a set size I want to downsize it to that size. Obviously it doesn't have to match exactly due to ratios, the width would be the key size so the height would be variable.

如果图像小于设定尺寸小,我想创建到集大小的新图像与定义的颜色的背景,然后集中上传的图片进去,因此结果与paddded彩色原稿。

If the image is smaller than the set size I would like to create a new image to the set size with a background of a defined colour and then centre the uploaded image into it therefore the result is the original with paddded colour.

大大AP preciated任何code例子或链接

Any code examples or links greatly appreciated

推荐答案

下面是code的片段我赶紧敲为调整其大小根据宽度。我相信你能弄清楚如何将背景颜色添加到该位图。这不是完整的code,但只是如何做的事情的想法。

Here's a snippet of code I quickly knocked up for resizing it based on the width. I'm sure you could figure out how to add a background color to the Bitmap. It's not complete code but just an idea of how to do things.

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

这篇关于ASP.Net MVC图片上传通过缩减或填充大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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