Solved

help me

Posted on 2006-11-02
6
489 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
has22 challenge 11 96
fizzArray2 challenge 1 80
scoreUp challenge 14 63
noX challenge 17 117
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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