Tuesday, February 19, 2013

Preserve image aspect ratio

I've been meaning to put some code snippets in here. Nothing earth shattering just little handy coding bits.

A while back I spent some time in xaml trying to maintain image aspect ratio. The images that I'm binding to vary in size and ratio. I knew the area they would be displayed  would have a height around 200-220 pixels. This is what I ended up doing.



Only bind to the dimension you don't know, width
Create a multibinding value converter. You do that by implimenting IMultiValueConverter.
Because these converters take object parameters, and I don't know who may use them after I am off a project; I always check the types of each param.

The math for getting ratio is just width / height.

If you know what height you want, then take the height and multiply it by the ratio
If you know the width take it and divide it by the ratio.

No comments:

Post a Comment