Solved

help me

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now