Solved

help me

Posted on 2006-11-02
6
512 Views
Last Modified: 2013-11-20
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  
0
Comment
Question by:taodo112
6 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 250 total points
ID: 17864282
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
 

Author Comment

by:taodo112
ID: 17872943
no, i want a source which i can load bitmap, draw on bitmap by pen or brush , and save it
0
 
LVL 1

Assisted Solution

by:Mazen
Mazen earned 250 total points
ID: 17980831
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question