0

I have implemented Uploadify in my ASP.NET MVC 3 application to upload images, but I now want to resize the images that I upload. I am not sure on what next to do in order to start resizing. I think there might be various ways to perform this resize, but I have not been able to find any example of this as yet. Can anyone suggest some way of doing this? Thanx

tereško
  • 58,060
  • 25
  • 98
  • 150
GatesReign
  • 826
  • 1
  • 9
  • 23

2 Answers2

2

Here's a function you can use on the server side. I use it to process my images after uploadify is done.

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

Here's how I use it:

        int length = (int)stream.Length;
        byte[] tempImage = new byte[length];
        stream.Read(tempImage, 0, length);

        var image = new Bitmap(stream);
        var resizedImage = ResizeImage(image, new Size(300, 300));

Holler if you need help getting it running.

rboarman
  • 8,248
  • 8
  • 57
  • 87
0

You have 3 ways:

  1. Use GDI+ library (example of code - C# GDI+ Image Resize Function)
  2. 3-rd part components (i use ImageMagick - my solution: Generating image thumbnails in ASP.NET?)
  3. Resize images on user side (some uploaders can do this)
Community
  • 1
  • 1
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45
  • How would you resize the image on client's side? You'd transfer the whole image and then shrink it by changing width / height via JS or is there something else you had in mind? – N.B. May 12 '11 at 22:11
  • on userside you can do it using some flash uploader or javauploader. I transfer full size image to server ant then convert it – Evgeniy Labunskiy May 12 '11 at 22:17