help me

i want to load file bitmap:
event Button Open:
void CDrawing2BitmapDlg::OnBnClickedBopen()
{
      
      
      // TODO: Add your control notification handler code here
      //xay dung bo loc cho open dialog
      static char BASED_CODE szFilter[]= "Bitmap File (*.Bmp)|*.bmp||";
      //tao hop hoi thoai open
      CFileDialog m_ldFile (TRUE,".bmp",m_sBitmap,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
      //xem hop hoai thoi va lay ket qua
      if (m_ldFile.DoModal()==IDOK)
      {
            //lay ten file duoc chon
            m_sBitmap=m_ldFile.GetPathName();
            //tai file bitmap vao bo nho
            //lay handle cua bitmap
            HBITMAP hBitmap =(HBITMAP)::LoadImage(AfxGetInstanceHandle(),m_sBitmap,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
            //ta da co gia tri handle cua bitmap
            //kiem tra cac dieu kien cua handle
            if (hBitmap)//neu chung ta da co handle
            {
                              //xoa anh bitmap hien thoi dang luu
                  if(m_bmpBitmap.DeleteObject());
                  //neu co mot anh thi hay go no ra
                  m_bmpBitmap.Detach();
                  //gang anh muon xem vao Bitmap Object
                  m_bmpBitmap.Attach(hBitmap);
                  //vo hieu hoa control Image
            }
            
      }
}
function showbitmap:
void CDrawing2BitmapDlg::ShowBitmap(CPaintDC* pdc,CWnd *pWnd)
{

      CDrawing2BitmapDlg *lpWnd=(CDrawing2BitmapDlg*)pWnd;
      BITMAP bm;
      //lay bitmap da load
      lpWnd->m_bmpBitmap.GetBitmap(&bm);
      CDC dcMem;
      // tao device context de load bitmap vao
      dcMem.CreateCompatibleDC(pdc);
      CBitmap *pOldBitmap=(CBitmap*)dcMem.SelectObject(lpWnd->m_bmpBitmap);
      CRect lRect;
      GetClientRect(lRect);
      lRect.NormalizeRect();
      pdc->StretchBlt(10,10,(lRect.Width()-20),(lRect.Height()-20),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
      dcMem.SelectObject(pOldBitmap);
      
}
 and in Onpaint:
void CDrawing2BitmapDlg::OnPaint()
{
      if (IsIconic())
      {
            CPaintDC dc(this); // device context for painting

            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      }
      else

      {
            CPaintDC dc(this);
            CDrawing2BitmapDlg* pWnd=(CDrawing2BitmapDlg*)GetParent();
            if(pWnd)
            {
            ShowBitmap(&dc,pWnd);
            CDialog::OnPaint();
            //Draw();
            }
      }
            
}
 

i don't know wrong anywhere , i can't load bitmap
if you have a source about MFC , program can load, draw on bitmap loaded and save that bimap, thank you  
taodo112Asked:
Who is Participating?
 
mahesh1402Connect With a Mentor Commented:
I am not sure where you want to display bitmap.. if you want to display bitmap on dialog then you dont need to retrieve handle of parent window.....

From your code you are calling ShowBitmap from OnPaint.. but your OnPaint handler is never invoked from OnBnClickedBopen().. to execute OnPaint() you need to call Invalidate() function from OnBnClickedBopen().

Code at last of OnBnClickedBopen() should be something like :

if (hBitmap)    
{    
if (m_bmpBitmap.DeleteObject())        
m_bmpBitmap.Detach();
m_bmpBitmap.Attach(hBitmap);    
}    

Invalidate(); <=== this will call OnPaint


Some sample links:
http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1691
http://www.codeguru.com/Cpp/G-M/bitmap/viewers/article.php/c1763/

-MAHESH
0
 
taodo112Author Commented:
no, i want a source which i can load bitmap, draw on bitmap by pen or brush , and save it
0
 
MazenConnect With a Mentor Commented:
Hi,

Here's a sample code to draw a bitmap on a dialog:

CClientDC* pDC = new CClientDC(this);
CDC* m_pMemDC = new CDC;
m_pMemDC->CreateCompatibleDC(pDC);
int m_nHRes = GetSystemMetrics(SM_CXSCREEN);
int m_nVRes = GetSystemMetrics(SM_CYSCREEN);
HBITMAP hBMP = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), "c:/image1.bmp",IMAGE_BITMAP,
0 ,  0 , LR_LOADFROMFILE);
DeleteObject(SelectObject(m_pMemDC->m_hDC, hBMP));
CRect lRect;
GetClientRect(lRect);
lRect.NormalizeRect();
BITMAP bm;
GetObject(hBMP, sizeof(BITMAP), &bm );
pDC->StretchBlt(10,10,(lRect.Width()-20),(lRect.Height()-20),m_pMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

/***********************/
To draw on a bitmap, u can use CClientDC class:

CClientDC* pDC = new CClientDC(this);
COLORREF color;
color = RGB(150,0,0);
CPen m_pen;
m_pen.CreatePen(PS_SOLID,1,color);
CPen* oldpen = pDC->SelectObject(&m_pen);
pDC->SelectStockObject(NULL_BRUSH);      //for hollow objects
pDC->Rectangle(0,0,250,250);
pDC->Ellipse(250,250,300,300);
//you can investigate the rest drawing functions alone and combine it with mouse events
/***********************/
To save a bitmap, you can capture the window as bitmap just like in the following website:
http://www.codeguru.com/Cpp/G-M/bitmap/capturing/article.php/c4919/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.