Here's an aspect ratio function I use written in C++, you can easily convert it I imagine.
inline CRect GetTargetRect(CRect rTarget, CSize szSource,
unsigned int uiAlign = DT_CENTER|DT_VCENTER)
{
float fLeft = (float)rTarget.left;
float fTop = (float)rTarget.top;
float fWidth = (float)rTarget.Width();
float fHeight = (float)rTarget.Height();
float fPercentCy = ((fHeight / szSource.cy) * 100);
float fPercentCx = ((fWidth / szSource.cx) * 100);
float fNewWidth = 0;
float fNewHeight = 0;
if((szSource.cx >= fWidth) || (szSource.cy >= fHeight))
{
if(fPercentCx > fPercentCy)
{
fNewWidth = ((szSource.cx * fPercentCy) / 100);
if(uiAlign & DT_CENTER)
fLeft += ((fWidth - fNewWidth) / 2);
else if(uiAlign & DT_RIGHT)
fLeft += (fWidth - fNewWidth);
fWidth = fNewWidth;
}
else
{
fNewHeight = ((szSource.cy * fPercentCx) / 100);
if(uiAlign & DT_VCENTER)
fTop += ((fHeight - fNewHeight) / 2);
else if(uiAlign & DT_BOTTOM)
fTop += (fHeight - fNewHeight);
fHeight = fNewHeight;
}
}
else
{
if(fPercentCx > fPercentCy)
{
float fNewWidth = ((szSource.cx * fPercentCy) / 100);
if(uiAlign & DT_CENTER)
fLeft += ((fWidth - fNewWidth) / 2);
else if(uiAlign & DT_RIGHT)
fLeft += (fWidth - fNewWidth);
fWidth = fNewWidth;
}
else
{
float fNewHeight = ((szSource.cy * fPercentCx) / 100);
if(uiAlign & DT_VCENTER)
fTop += ((fHeight - fNewHeight) / 2);
else if(uiAlign & DT_BOTTOM)
fTop += (fHeight - fNewHeight);
fHeight = fNewHeight;
}
}
return CRect((int)fLeft, (int)fTop, (int)(fLeft + fWidth), (int)(fTop + fHeight));
}