How to Resize an Image in ASP.NET?

Last post 06-16-2005 7:43 AM by JasonFollas. 2 replies.

Sort Posts:

  • How to Resize an Image in ASP.NET?

    06-15-2005, 5:26 PM
    • Contributor
      3,569 point Contributor
    • shapper
    • Member since 11-28-2004, 9:15 PM
    • Posts 2,947
    Hello,

    In each record of an ASP.Net Repeater I have an image which original size is 200px width and with a height which can vary from record to record.

    How can I resize each image to 50px and keep its proportion?

    Does anyone know how to do this?

    Thank You,
    Miguel
  • Re: How to Resize an Image in ASP.NET?

    06-15-2005, 10:16 PM
    • All-Star
      54,795 point All-Star
    • DarrellNorton
    • Member since 04-04-2003, 3:49 PM
    • VA, USA
    • Posts 6,595
    • Moderator
      TrustedFriends-MVPs
    Darrell Norton, MVP
    Darrell Norton's Blog


    Please mark this post as answered if it helped you!
  • Re: How to Resize an Image in ASP.NET?

    06-16-2005, 7:43 AM
    • Participant
      860 point Participant
    • JasonFollas
    • Member since 06-02-2005, 2:13 PM
    • 41.501000, -83.718000
    • Posts 170

    Your width is a constant reduction (25% of original size), which makes it slightly easier rather than trying to take any dimension and scale to 50px wide...

    1. Load the original into an Image object, and get the height.
    2. Create a new Bitmap with a width and height that is 25% of original values.
    3. Get a Graphics object for the bitmap.
    4. Draw the original onto the bitmap, specifying a rectangle the size of the bitmap
    5. Save file (or do whatever you need to do with it).
    6. Dispose objects.

    System.Drawing.Image original = System.Drawing.Image.FromFile(@"c:\10yearsreunion.jpg");

    int
    height = original.Height;
    int width = original.Width;

    System.Drawing.Bitmap newPic = new System.Drawing.Bitmap(width/4, height/4);

    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(newPic);
    gr.DrawImage(original, 0, 0, (width/4), (height/4));

    newPic.Save(@"c:\10yearsreuinion_25pct.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    gr.Dispose();
    newPic.Dispose();
    original.Dispose();



     

Page 1 of 1 (3 items)