简体   繁体   中英

Image resize asp.net mvc application

POST EDITED ADDED THE LINK

Read a very good post on image resize [here][1] in asp.net mvc.

http://dotnetslackers.com/articles/aspnet/Testing-Inbound-Routes.aspx

I need this logic to work for the images that are uploaded in cdn also.Say for example i have uploaded an image in cdn and now i want to fetch it from my controller and resize it.Also the image should not be saved in my server as it will not be good idea as it consumes valuable resource.The image has to be read from CDN and re sized without saving it locally in server.How can we achieve this using the methodology given in the above post.

Thanks, S.

If you use ASP.Net MVC3, you can try new helper - WebImage.

This is my test code.

    public ActionResult GetImg(float rate)
    {
        WebClient client = new WebClient();
        byte[] imgContent = client.DownloadData("ImgUrl");
        WebImage img = new WebImage(imgContent);
        img.Resize((int)(img.Width * rate), (int)(img.Height * rate));
        img.Write();

        return null;
    }

You can use the GDI+ features in the System.Drawing namespace

Bitmap newBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)newBitmap);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(sourceImage, 0, 0, destWidth, destHeight);
g.Dispose();

Here's what I use. Works great.

    private static Image ResizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM