Skip to main content

image resizing in asp.net 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);
    image.Dispose();

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

}

In asp.net 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)
{
  try
  {
   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));
  }
  catch
  {
           
  }

}


Download image re-sizing sample code click here.

Popular posts from this blog

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 -

Merging multiple PDFs using iTextSharp in asp.net 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 to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".