Skip to main content

image resizing in c# dynamically

In this article you can find the code that is for re-sizing images with given width and height and retain the quality of the image as it was.

below is the function just pass new width, new height and original image file path it will save resize given image and save it into the folder named newsizeimages and returns the new path.

Resize Image Code C#

public string CreateThumbnail(int maxWidth, int maxHeight, string path)

    var image = System.Drawing.Image.FromFile(path);
    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 thumbGraph = Graphics.FromImage(newImage);

    thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
    thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
    //thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

    thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);

    string fileRelativePath = "newsizeimages/" + maxWidth + Path.GetFileName(path);
    newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
    return fileRelativePath;


In if you want to resize client uploaded image files directly when client uploading their image files then you have to save this files first into the server and then pass this file path into the above function as given below example.
if (updImage.HasFile)
   updImage.SaveAs(Server.MapPath("uploadedimage/" + updImage.FileName));

   imgCustomTo8.ImageUrl = CreateThumbnail(8, 8, Server.MapPath("uploadedimage/" + updImage.FileName));
   imgCustomTo16.ImageUrl = CreateThumbnail(16, 16, Server.MapPath("uploadedimage/" + updImage.FileName));
   imgCustomTo24.ImageUrl = CreateThumbnail(24, 24, Server.MapPath("uploadedimage/" + updImage.FileName));
   imgCustomTo32.ImageUrl = CreateThumbnail(32, 32, Server.MapPath("uploadedimage/" + updImage.FileName));


Download image re-sizing sample code click here.

Popular posts from this blog

Merging multiple PDFs using iTextSharp in c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Uploading large file in chunks in Mvc c# from Javascript ajax

Often we have a requirement to upload files in, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Scrollable Gridview with fixed headers in

horizontal and vertical scrollable Gridview with static header row.fixed header in gridview in header in gridview through java script.I was looking for a solution for this for a long time and found most of the answers are not working or not suitable for my situation i also find most of the java script code for that they worked but only with the vertical scroll not with the horizontal scroll and also combination of header and rows doesn't match.