To display an image with the correct aspect ratio on a button, you must first calculate a common scaling factor, rescale the image with it and finally crop it. The following function performs these steps. The image can be a file or a resource e.g. in jpg format. I have tested the function with an image that is larger than the button.
void RezizeImage(CImage& img_src, CImage& img_dst, int dstW, int dstH)
{
img_dst.Create(dstW, dstH, 32);
HDC hdc_src = img_src.GetDC();
HDC hdc_dst = img_dst.GetDC();
if (!hdc_src || !hdc_dst) return;
int srcW = img_src.GetWidth();
int srcH = img_src.GetHeight();
double s = max((double)dstW / srcW, (double)dstH / srcH);
srcW *= s; srcH *= s;
int ofsH=0, ofsW = 0;
if (srcH != dstH)
ofsH = (dstH - srcH) / 2;
else
ofsW = (dstW - srcW) / 2;
SetStretchBltMode(hdc_src, COLORONCOLOR);
SetStretchBltMode(hdc_dst, COLORONCOLOR);
img_src.StretchBlt(hdc_src, 0, 0, srcW, srcH, SRCCOPY); img_src.BitBlt(hdc_dst, ofsW, ofsH, SRCCOPY);
img_src.ReleaseDC();
img_dst.ReleaseDC();
}
It may also be possible to optimize the program.