Hi i am trying to display a Image & Rotate and Display it in my window using win32 application. Ref[
http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1743/Rotate-a-bitmap-image.htm[
^]
My problem is the Image is appearing as a Monochrome Image & not a color Image. Otherwise this drawing operation fails returning true or false.
Could anyone please point the bug in my code?
Thank you
Regards,
Balaji.R
HBITMAP GetRotatedBitmap( HWND hWnd,HBITMAP hBitmapSource, float radians)
{
radians = (2 * 3.14567 *radians)/360;
HDC sourceDC, destDC;
sourceDC = CreateCompatibleDC(NULL);
destDC = CreateCompatibleDC(NULL);
COLORREF clrBack=0;
BITMAP bm;
GetObject(hBitmapSource,sizeof( bm ), &bm );
float cosine = (float)cos(radians);
float sine = (float)sin(radians);
int x1 = (int)(bm.bmHeight * sine);
int y1 = (int)(bm.bmHeight * cosine);
int x2 = (int)(bm.bmWidth * cosine + bm.bmHeight * sine);
int y2 = (int)(bm.bmHeight * cosine - bm.bmWidth * sine);
int x3 = (int)(bm.bmWidth * cosine);
int y3 = (int)(-bm.bmWidth * sine);
int minx = min(0,min(x1, min(x2,x3)));
int miny = min(0,min(y1, min(y2,y3)));
int maxx = max(0,max(x1, max(x2,x3)));
int maxy = max(0,max(y1, max(y2,y3)));
int w = maxx - minx;
int h = maxy - miny;
HBITMAP hbmResult = CreateCompatibleBitmap(destDC, w, h);
HBITMAP hbmOldSource = (HBITMAP)SelectObject( sourceDC, hBitmapSource );
HBITMAP hbmOldDest = (HBITMAP)SelectObject( destDC, hbmResult );
HBRUSH hbrBack = CreateSolidBrush( clrBack );
HBRUSH hbrOld = (HBRUSH)SelectObject( destDC, hbrBack );
PatBlt(destDC,0,0, w, h, SRCCOPY );
DeleteObject(hbrOld);
SetGraphicsMode(destDC, GM_ADVANCED);
XFORM xform;
xform.eM11 = cosine;
xform.eM12 = -sine;
xform.eM21 = sine;
xform.eM22 = cosine;
xform.eDx = (float)-minx;
xform.eDy = (float)-miny;
SetWorldTransform( destDC, &xform );
BitBlt(destDC, 0, 0,bm.bmWidth, bm.bmHeight,sourceDC, 0, 0, SRCCOPY );
SelectObject( sourceDC, hbmOldSource );
SelectObject( destDC, hbmOldDest );
return hbmResult;
}
void DrawImageWindow(HWND hWnd,float angle)
{
HDC hDC = GetWindowDC(hWnd);
HBITMAP hBitmapDestination;
HBITMAP hBitmapSource = (HBITMAP)LoadImage(NULL, L"SourceImage.bmp", IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
hBitmapDestination = (HBITMAP)GetRotatedBitmap(hWnd,hBitmapSource,angle);
BITMAP BMP;
GetObject(hBitmapDestination, sizeof(BMP), &BMP);
HDC BMPDC = CreateCompatibleDC(NULL);
SelectObject(BMPDC, hBitmapDestination);
BitBlt(hDC, 0, 0, BMP.bmWidth, BMP.bmHeight, BMPDC, 0, 0, SRCCOPY);
ReleaseDC(hWnd,hDC);
DeleteDC(BMPDC);
DeleteObject(hBitmapSource);
DeleteObject(hBitmapDestination);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
DrawImageWindow(hWnd,angle);
EndPaint(hWnd, &ps);
break;
....
}