Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to rotate a bitmap image in C++ using MFC?
I dont want to use GDI. Is it possible by only changing x and y values in this code?
C++
CBitmap img;
CDC dc;
BITMAP bmp;
img.LoadBitmapW(IDB_BITMAP1);
img.GetBitmap(&bmp);
CDC* pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
CBitmap* pOld = dc.SelectObject(&img);
for(int y = 0; y < bmp.bmHeight; y++)
{
    for(int x = 0; x < bmp.bmWidth; x++)
    {
        COLORREF rgb = dc.GetPixel(x, y);
        BYTE r = GetRValue(rgb);
        BYTE g = GetGValue(rgb);
        BYTE b = GetBValue(rgb);
        dc.SetPixel(x, y, RGB(r,g,b));
    }
}
pDC->BitBlt(200, 200, bmp.bmWidth, bmp.bmHeight, &dc, 0, 0, SRCCOPY);
dc.SelectObject(pOld);


Please someone reply soon, as this is the last day to work on project, tomorrow is its submission.
Posted
Updated 27-Jun-10 20:46pm
v3
Comments
Sandeep Mewara 28-Jun-10 1:53am    
Have patience!

1 solution

Why don't you just try ??

To me you seems soooo confused ... that you deserve to fail your project submission.

1) Why do you split the COLORRF into its component if you than re-put them together without doing any operation on them?

2) If you write a pixel the new position, the old pixel will be replaced. But you still need to read that pixel later...

3) A rotation implies that what is "wide" becomes "long". Where do you store (y,x) if (x,y) has x > height or y > width ?

Better to create a destination image different than the source, with appropriate sizes.
 
Share this answer
 
Comments
Sweety Khan 28-Jun-10 3:52am    
oh i learned so much from ur first question and feeling so stupid.
Sweety Khan 28-Jun-10 3:55am    
i got ur point, i have to make two bitmaps one is source and other is destination.
Sweety Khan 28-Jun-10 4:05am    
but how can i make destination bitmap? here in the code it gets the size by itself. how can i specify the size of new bitmap? here i used getdc, createcompatible etc but how i used all these for this new one. plz show me, i was trying to make a new bitmap for other features too but still unsuccessful :(
Emilio Garavaglia 28-Jun-10 7:45am    
if "rotate" means 90° rotation, the new sizes will have cx and cy exchanged (and yes ... at that point you can copy pixels exchanging x and y - but you probably have to offset and invert one of the axis, depending on the side of the rotation).

If "rotate" means "any angle", well, at least a bit of trigonometry is needed (and also some interpolation).

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