Answered by:
Maintain aspect ratio of an Image in a webform

Question
-
User973532244 posted
Hi,
My back end C# code gets the URL of an Image and the image is displayed in the ASPX page using an Image Control. The Image needs to be displayed within a certain size, for example the image needs to be contained within a 200px X 200px DIV. I've tried setting the width and height properties of the image control but this does not maintain the aspec ratio of the image when it's loaded. Is there a way I can dynamically display an image and maintain it's aspec ratio?
cheers
Thursday, November 27, 2008 6:05 AM
Answers
-
User187056398 posted
If you only set one dimension of an ImageControl, the aspect ratio is maintained.
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/April.JPG" /> <asp:Image ID="Image2" runat="server" Height="50px" ImageUrl="~/Images/April.JPG" /> <asp:Image ID="Image3" runat="server" Width="30px" ImageUrl="~/Images/April.JPG" />
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 27, 2008 12:49 PM
All replies
-
User187056398 posted
If you only set one dimension of an ImageControl, the aspect ratio is maintained.
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/April.JPG" /> <asp:Image ID="Image2" runat="server" Height="50px" ImageUrl="~/Images/April.JPG" /> <asp:Image ID="Image3" runat="server" Width="30px" ImageUrl="~/Images/April.JPG" />
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 27, 2008 12:49 PM -
User973532244 posted
thanks very much, works great
Friday, November 28, 2008 4:44 AM -
User977725702 posted
I think it's not a good idea to resize image by specify Width or Heigh on a Image directly .. You might use an HttpHandler to reduce Image Size and Maintain Aspet Ratio.
Take a look at this thread : http://forums.asp.net/t/1349134.aspx
Saturday, November 29, 2008 10:10 AM -
User187056398 posted
I think it's not a good idea to resize image by specify Width or Heigh on a Image directlyWhy not?
Saturday, November 29, 2008 10:58 AM