最近工作中利用MFC实现图片旋转的任务,发现网上这个资料比较少,也比较杂。最终自己也是看了好多代码后,将代码融合了一下,最终能够满足自己的使用要求。
1.旋转函数
//hBitmap:要旋转位图的句柄
//radians : 旋转的角度(弧度)
//clrBack :旋转时的背景颜色
//旋转图片函数
HBITMAP GetRotatedBitmap(HBITMAP hBitmap, float radians, COLORREF clrBack)
{
// Create a memory DC compatible with the display
CDC sourceDC, destDC;
sourceDC.CreateCompatibleDC(NULL);
destDC.CreateCompatibleDC(NULL);
// Get logical coordinates
BITMAP bm;
::GetObject(hBitmap, sizeof(bm), &bm);
float cosine = (float)cos(radians);
float sine = (float)sin(radians);
int w = bm.bmWidth;
int h = bm.bmHeight;
// Create a bitmap to hold the result
HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), w, h);
HBITMAP hbmOldSource = (HBITMAP)::SelectObject(sourceDC.m_hDC, hBitmap);
HBITMAP hbmOldDest = (HBITMAP)::SelectObject(destDC.m_hDC, hbmResult);
// Draw the ba