Hello All,
I am developing a paint like application in which user can insert the .bmp file and can resize and rotate.
So when i rotate the .bmp then then the .bmp inserted disappears and whole screen attains the default color of the dialog.
This problem has not been come in case of 0 degree rotation.
I am using virtual window concept in my application to paint the screen.
My code is as below :
Class variables :
protected:
CDC m_memDC;
int maxX,maxY;
CBitmap m_bmp;
CBitmap m_bitmap;
CBrush m_bkbrush;
void CRotateBitmapView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
maxX = GetSystemMetrics(SM_CXSCREEN);
maxY = GetSystemMetrics(SM_CYSCREEN);
CClientDC DC(this);
COLORREF crBkgnd = RGB(255,255,255);
m_memDC.CreateCompatibleDC(&DC);
m_bkbrush.CreateSolidBrush(crBkgnd);
m_bmp.CreateCompatibleBitmap(&DC,maxX,maxY);
m_memDC.SelectObject(&m_bmp);
m_memDC.SelectObject(&m_bkbrush);
m_memDC.PatBlt(0,0,maxX,maxY,PATCOPY);
}
void CRotateBitmapView::OnDraw(CDC* pDC)
{
pDC->BitBlt(0, 0, maxX, maxY, &m_memDC, 0, 0, SRCCOPY);
}
void CRotateBitmapView::OnBnClickedButton5()
{
BITMAP bm;
m_bitmap.DeleteObject();
m_bitmap.LoadBitmapW(IDB_BITMAP1);
HBITMAP hBitmap = (HBITMAP)m_bitmap.GetSafeHandle();
switch(m_Combo.GetCurSel())
{
case 1:
GetRotatedBMP(hBitmap,90.0f);
break;
case 2:
GetRotatedBMP(hBitmap,180.0f);
break;
case 3:
GetRotatedBMP(hBitmap,270.0f);
break;
default:
GetRotatedBMP(hBitmap,0.0f);
break;
}
}
void CRotateBitmapView::GetRotatedBMP(HBITMAP hBitmap,double angle)
{
CClientDC m_memDC(this);
BITMAP bm;
m_bitmap.GetBitmap(&bm);
CDC memdc;
memdc.CreateCompatibleDC(&m_memDC);
memdc.SelectObject(&m_bitmap);
CClientDC dc(this);
COLORREF rgb = RGB(255,255,255);
CBrush brush ;
brush.CreateSolidBrush(rgb);
dc.FillRect(CRect(0,0,bm.bmWidth,bm.bmHeight),&brush);
dc.FillRect(CRect(0,0,bm.bmHeight,bm.bmWidth),&brush);
const float PI = 3.141529f;
const float radians = (float)angle / (360.0f / (2 * PI));
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)));
XFORM xf;
xf.eM11 = cosine;
xf.eM12 = -sine;
xf.eM21 = sine;
xf.eM22 = cosine;
xf.eDx = (float)-minx;
xf.eDy = (float)-miny;
::SetGraphicsMode(m_memDC.m_hDC, GM_ADVANCED);
::SetWorldTransform(m_memDC.m_hDC, &xf);
m_memDC.StretchBlt(0,0,bm.bmWidth,bm.bmHeight,&memdc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
InvalidateRect(NULL);
}
Please help....
Regards Gurmeet Singh