tags:

views:

58

answers:

1

how to rotate a bitmap image in c++ using MFC? i dont wanna use GDI. is it possible by only changing x and y values in this code?

  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, tommorrow is its submission.

+1  A: 

Asking about doing drawing with MFC but not using GDI is a bit like asking about how to go swimming without getting wet. As far as drawing goes, MFC is a thin wrapper around GDI, so anything you do with MFC gets translated quite directly to GDI with just a bit of syntactic sugar added (and in this area, the amount of syntactic sugar was based on a diabetic's diet).

That said, yes, exchanging x and y in your loops could do roughly the right thing (depending on the direction of rotation you want, for one thing) -- though in all honesty you should think really hard about scrapping that code completely. You're using SetPixel (i.e., GDI) to do the drawing in any case; there are lots better ways to do it than this (from the looks of things, you could benefit immensely from CreateDIBSection).

Jerry Coffin
u r absolutely right Jerry. actually my time is up. i think i cant learn GDI in the remaining time. thats why doing rotation like this. once i was working on c and just by a single mistake of > i got rotated image but wht a luck now i m trying and dont get it
Sweety Khan
The easiest way to rotate the image will probably be to draw it normally, but call `SetWorldTransform` to set up the rotation.
Jerry Coffin