2

I need a code that will allow me to resize images, but with the following functionality:

1) resize image upon upload

2) Resize image proportionally by specifying either height or width.

Note:

  • Should be done in ASP.NET C#

For example: The function should get the a width OR a height, and resize the image proportionally for the give height OR Width. Let's say that the image is 400(w)x100(h). I want to tell the function to resize the image to a specific height, let's say 80px. The function should resize the image proportionally while setting the image height to 80px and the width accordingly. Another option is ti tell the function the width, let's say 200px, and the function should resize the image to 200px width and set the height accordingly.

3) save the image to a specific location (path).

4) Function can work with uploaded image or by specifying an image path.

5) I want to be able to choose the image quality

6) Only need this for JPEG

Can somebody please help me out with this. Thanks.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • 2
    How much of this have you done? There are code samples all over the Internet that will show you how to do each and every one of these things. – David Jul 27 '11 at 17:19
  • For example: http://www.codeproject.com/KB/cs/ImgSizingFunc.aspx – David Jul 27 '11 at 17:22
  • [This is a good example I found with a simple google search.](http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx) The header under custom resizing, I think is what you're looking for, allowing you to specify quality. – Christopher Currens Jul 27 '11 at 17:23
  • 1
    [Or here on SO, this covers a lot of it.](http://stackoverflow.com/questions/6777054/rendering-smallest-possible-image-size-with-mvc3-vs-webforms-library) I really wish people would put in a minimum of effort before asking. – Tridus Jul 27 '11 at 17:28
  • I wrote a asp.net handler that does most of what you want: http://stackoverflow.com/questions/4436209/asp-net-version-of-timthumb-php-class/4506072#4506072 – Peter Jul 27 '11 at 17:28
  • Question specifies non-mvc and c#... – IrishChieftain Jul 27 '11 at 17:34
  • possible duplicate of [Resizing an image in asp.net without losing the image quality](http://stackoverflow.com/questions/2319983/resizing-an-image-in-asp-net-without-losing-the-image-quality) – Simone Carletti Nov 17 '11 at 09:28
  • possible duplicate of [Resize JPEG image to fixed width, while keeping aspect ratio as it is](http://stackoverflow.com/questions/8214562/resize-jpeg-image-to-fixed-width-while-keeping-aspect-ratio-as-it-is) – Rowland Shaw Jul 19 '12 at 07:44

4 Answers4

5

Taken from this Stackoverflow answer, I come up with:

public static Image Resize(this Image image, int maxWidth = 0, int maxHeight = 0)
    {
        if (maxWidth == 0)
            maxWidth = image.Width;
        if (maxHeight == 0)
            maxHeight = image.Height;

        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);

        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;
    }

To resize Image specifying its maxWidth:

var _image = Image.FromStream(Source);
var _thumbImage = _image.Resize(100);

To resize Image specifying its maxHeight:

var _image = Image.FromStream(Source);
var _thumbImage = _image.Resize(maxHeight: 100);
Community
  • 1
  • 1
stack247
  • 5,579
  • 4
  • 41
  • 64
5

Although it seems like you should be able to copy and paste a snippet to do this, there are a ton of pitfalls you need to look out for if you're building your own image resizing system. It's better to use a proven, tested, and supported open-source library.

To resize to a file directly from HttpPostedFile, call

ImageBuilder.Current.Build(httpPostedFile, "img.jpg", new ResizeSettings("width=200&quality=90"));

To resize an existing file, call

ImageBuilder.Current.Build("orig.jpg", "img.jpg", new ResizeSettings("width=200&quality=90"));

The ImageResizing.Net library is free, and MIT-licensed (no worries about licensing problems).

Community
  • 1
  • 1
Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • The link to your resizing pitfalls blog post is broken. For anyone looking for it, there is a [copy of it on archive.org](https://web.archive.org/web/20141101232125/http://nathanaeljones.com/blog/2009/20-image-resizing-pitfalls) – JoelWilson Aug 26 '15 at 16:51
5

Last day I found imageresizer and its great. and good API. Works Great. Downloaded from Visual studio 2010 Extension Manager: http://nuget.org/.

Easy Steps to download API in VS-2010:

1). Install Extension http://nuget.org/.

enter image description here

3). Find and Install ImageResizing
enter image description here

4).Then Code: (I m using here cropping. you can use any) Documentation on imageresizing.net

string uploadFolder = Server.MapPath(Request.ApplicationPath + "images/");
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);


//The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
ResizeSettings resizeCropSettings = new ResizeSettings("width=200&height=200&format=jpg&crop=auto");

//Generate a filename (GUIDs are safest).
string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString());

//Let the image builder add the correct extension based on the output file type (which may differ).
fileName = ImageBuilder.Current.Build(uploadFolder + FileUpload1.FileName, fileName, resizeCropSettings, false, true);

Try!!! its very awsumm and easy to use. thanks.

slugster
  • 49,403
  • 14
  • 95
  • 145
Muhammad Adnan
  • 1,373
  • 1
  • 13
  • 20
  • The imageresizer URL you used was incorrect, I've fixed it. Your answer may be perfectly valid for the question, but please don't copy/paste the same answer onto multiple questions, you could just write it once and then leave comments with a link back to it on other questions. – slugster Nov 17 '11 at 09:36
0

This is how is did in my project

On Button click while uploading file:

System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
     System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);
     objImage.Save(SaveLocation,ImageFormat.Png);
     lblmsg.Text = "The file has been uploaded.";

public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
        {
            var ratio = (double)maxHeight / image.Height;
    
            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);
    
            var newImage = new Bitmap(newWidth, newHeight);
            using (var g = Graphics.FromImage(newImage))
            {
                g.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            return newImage;
        }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102