Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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

C++
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;

	// Get logical coordinates
    BITMAP bm;
	GetObject(hBitmapSource,sizeof( bm ), &bm );
	
	float cosine = (float)cos(radians);
	float sine = (float)sin(radians);
 		
	// Compute dimensions of the resulting bitmap
	// First get the coordinates of the 3 corners other than origin
	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;
			
	// Create a bitmap to hold the result
	HBITMAP hbmResult = CreateCompatibleBitmap(destDC, w, h);	     
	HBITMAP hbmOldSource = (HBITMAP)SelectObject( sourceDC, hBitmapSource );	
    HBITMAP hbmOldDest = (HBITMAP)SelectObject( destDC, hbmResult );
     
	// Draw the background color before we change mapping mode
    HBRUSH hbrBack = CreateSolidBrush( clrBack );
    HBRUSH hbrOld = (HBRUSH)SelectObject( destDC, hbrBack );
	PatBlt(destDC,0,0, w, h, SRCCOPY );
    DeleteObject(hbrOld);

	// We will use world transform to rotate the bitmap
	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 );// Finally, Draw it
   	
	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); // Here we get the BMP header info.


	HDC BMPDC = CreateCompatibleDC(NULL); // This will hold the BMP image itself.

	
	SelectObject(BMPDC, hBitmapDestination); // Put the image into the DC.

	BitBlt(hDC, 0, 0, BMP.bmWidth, BMP.bmHeight, BMPDC, 0, 0, SRCCOPY); // Finally, Draw it

	ReleaseDC(hWnd,hDC);

	// Don't forget to clean up!
	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;
	....
}
Posted
Updated 28-Aug-13 22:30pm
v4

1 solution

I have fixed this bug by changing this line.
HBITMAP hbmResult = CreateCompatibleBitmap(destDC, w, h);
to
HBITMAP hbmResult = CreateCompatibleBitmap(GetDC(NULL), w, h);//
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900