In an HTML window, I am setting a custom body
<img src="file://[filename]">
to display an image.
Now I want to strectch the image to fit the available window, but preserve aspect ratio.
<img src="file://[filename]" width="100%" height="100">
stretches but also distorts the image.
Is there some HTML trickery to do that? IE only solution is fine, since this is in a hosted broweser control.
You can preserve the aspect ratio by specifying only width and setting height to auto using CSS property. This will render a 400px wide image. The height is adjusted accordingly to preserve the aspect ratio of the original image.
In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.
By setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained. The end result is an image that scales up or down perfectly.
The max-height property sets the maximum height of an element, and the max-width property sets the maximum width of an element. To resize an image proportionally, set either the height or width to "100%", but not both. If you set both to "100%", the image will be stretched.
If you want to preserve aspect ratio, you only need to specify one dimension ie:
<img src="file://[filename]" width="100%" alt="" />
You can use javascript to determine max dimension and resize accordingly
<script type="text/javascript">
function getImgSize(id){
var pic = document.getElementById(id);
var h = pic.offsetHeight;
var w = pic.offsetWidth;
alert ('The image size is '+w+'*'+h);
}
</script>
If you know either the height or width, you can set only that. The other dimension will be automatically set based on the aspect ratio of the image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With