Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help me

Posted on 2006-11-02
6
Medium Priority
?
551 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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 this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 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