locked
How to Resize an Image in ASP.NET? RRS feed

  • Question

  • User1608082067 posted
    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
    Wednesday, June 15, 2005 5:26 PM

All replies

  • User-837620913 posted
    Here's one article:
    Creating Thumbnail Images on the fly with ASP.NET

    Wednesday, June 15, 2005 10:16 PM
  • User1152494133 posted

    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();



     

    Thursday, June 16, 2005 7:43 AM