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 ?
-
See stackoverflow.com/questions/647377/… for a very similar questiondash– dash2012-04-02 09:03:40 +00:00Commented Apr 2, 2012 at 9:03
-
Thanks dash. But I am using asp.net Image control. I need to accomplish the same thing, re size the image just for displaySenan– Senan2012-04-02 09:14:10 +00:00Commented Apr 2, 2012 at 9:14
-
You can still use your image control; however, the images themselves could be loaded via a handler by setting the source appropriately.dash– dash2012-04-02 10:10:25 +00:00Commented Apr 2, 2012 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 MaxHeightSenan– Senan2012-04-02 10:21:16 +00:00Commented Apr 2, 2012 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– dash2012-04-02 10:44:02 +00:00Commented Apr 2, 2012 at 10:44
Add a comment
|
1 Answer
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 */