0

I have created an asp.net composite control that consist of a Label, Text box and an Image control.I need to display the image in Image control without loosing aspect ratio.Maximum width and height of the image control is 640x480 . all the uploaded images are having resolution greater than 640x480. How can I scale the image to fit within the Image control.Is there any way to accomplish this without re size the image and saving it temporarily ?

Senan
  • 411
  • 2
  • 6
  • 16
  • See http://stackoverflow.com/questions/647377/resize-and-display-image-from-server-with-asp-net for a very similar question – dash Apr 02 '12 at 09:03
  • Thanks dash. But I am using asp.net Image control. I need to accomplish the same thing, re size the image just for display – Senan Apr 02 '12 at 09:14
  • You can still use your image control; however, the images themselves could be loaded via a handler by setting the source appropriately. – dash Apr 02 '12 at 10:10
  • thanks dash. I'vs solved the issue by adding some code in OnPreRender.using (System.Drawing.Image MyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(ImageUrl))) if (MaxWidth != 0 && CurrentWidth > MaxWidth){ CurrentWidth = MaxWidth;ImgCtrl.Attributes.Add("Width", CurrentWidth.ToString());} else if (MaxHeight != 0 && CurrentHeight > MaxHeight){ CurrentHeight = MaxHeight; ImgCtrl.Attributes.Add("Height", CurrentHeight.ToString()); } I used predefined value for MaxWidth and MaxHeight – Senan Apr 02 '12 at 10:21
  • Cool; however, remember the images will still be the same file size, and may not resize as gracefully as you want. – dash Apr 02 '12 at 10:44

1 Answers1

0

I was using this css class for client-side resizing of images for my thumbnail display.

.css-class-name{
        max-width: 250px; /*works on some browsers*/ 
        max-height: 183px;
        width: expression(this.width > 250 ? "250px" : true);  
        height: expression(this.height > 183 ? "183px" : true); /*long image handling, */
    }        
/*Source: http://www.fastechws.com/tricks/web/image-max-width-height.php */
Ozgur Ozturk
  • 1,265
  • 11
  • 9