2

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.

1
  • You are missing the link to the post. Can you add it? Commented Mar 9, 2012 at 20:03

3 Answers 3

9

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.