Click here to Skip to main content
15,891,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to rotate images via SetWorldTransform, but its not working...

Below is the code i have written. It is in its simplest form, but still cant make it to work.. any help appreciated in advance..

int iAngleInDeg = 90;
HBITMAP hBitmap;
CString m_csBitmap;
m_csBitmap = "C://Aircraft.bmp";
hBitmap = (HBITMAP)LoadImage(NULL,m_csBitmap.GetBuffer(m_csBitmap.GetLength()),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
CDC sourceDC, destDC;
sourceDC.CreateCompatibleDC( NULL );
destDC.CreateCompatibleDC( NULL );
// Get logical coordinates
BITMAP bm;
::GetObject( hBitmap, sizeof( bm ), &bm );

float radians = radians=(2*3.1416*iAngleInDeg)/360; 
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(CClientDC(NULL), w, h);
HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );
// Draw the background color before we change mapping mode
HBRUSH hbrBack = CreateSolidBrush( RGB(0,0,0) );
HBRUSH hbrOld = (HBRUSH)::SelectObject( destDC.m_hDC, hbrBack );
//destDC.PatBlt( 0, 0, w, h, PATCOPY );
::DeleteObject( ::SelectObject( destDC.m_hDC, hbrOld ) );
// We will use world transform to rotate the bitmap
SetGraphicsMode(destDC.m_hDC, 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.m_hDC, &xform );

	
// Now do the actual rotating - a pixel at a time
destDC.BitBlt(0,0,bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY) ;
//now draw the rotated bitmap on final DC, i.e. pDC
pDC->BitBlt(0, 0,bm.bmWidth, bm.bmHeight, &destDC, 0, 0, SRCCOPY);
	
	
// Restore DCs
::SelectObject( sourceDC.m_hDC, hbmOldSource );
::SelectObject( destDC.m_hDC, hbmOldDest );
Posted
Updated 15-Feb-10 0:13am
v2

As I understand, you've already look at article suggested in my answer to your previous question about the same thing (rotating a figure). So you HAVE TO BE SURE that SetWorldTransform and BitBlt works fine.

What does it mean "not working"???
 
Share this answer
 
By not working, i mean that i have implemented the code as according to my understanding and that it should work. But if you run the code, the source DC is not rotated. Thats why I wrote "Not Working". Can you try the code and suggest a solution?

Thanks
 
Share this answer
 
The Problem happens when the bitmap is to large, reduce the size as in my sample
C#
#define	MAXBLTSIZEX	800	
#define	MAXBLTSIZEY	800	

BitBlt(CDC *pDC, int x, int y, int nWidth, int nHeight, CDC *pSrcDC, int xSrc, int ySrc, DWORD dwRop)
int xFrom,yFrom ;
int xSrcFrom ,ySrcFrom  ;
int w   ;
int h   ;
for (xFrom = x;xFrom < x+nWidth;xFrom   +=  MAXBLTSIZEX)
{
    xSrcFrom    =   xSrc    +   (xFrom - x) ;
    w   =   nWidth  -   (xFrom - x) ;
    if (w > MAXBLTSIZEX)
        w   =   MAXBLTSIZEX ;
    for (yFrom=y;yFrom < y + nHeight;yFrom +=   MAXBLTSIZEY)
    {
        ySrcFrom    =   ySrc    +   (yFrom - y) ;
        h   =   nHeight -   (yFrom - y) ;
        if (h > MAXBLTSIZEY)
            h   =   MAXBLTSIZEY ;
        pDC->BitBlt (xFrom,yFrom,w,h,pSrcDC,xSrcFrom,ySrcFrom,dwRop)    ;
    }

}


regards

Heinz
 
Share this answer
 

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

  Print Answers RSS


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