Link to home
Start Free TrialLog in
Avatar of polimetla
polimetla

asked on

How to copy bitmap from clipboard to view?

Please help me...

1. How to copy the bitmap from clipboard to view?
2. How to save this bimap as .bmp file?

I tried the following code in OnDraw().
BitBlt function returning 0.
       
BOOL dFlag;
HANDLE hBitmap;      
HDC hdcDest;
DWORD dwError;

dFlag = ::OpenClipboard(m_hWnd);             
hBitmap = ::GetClipboardData(CF_DIB);                  
hdcDest = ::GetDC(m_hWnd);
dFlag = ::BitBlt((HDC)pDC,0,0,800,600,(HDC)hBitmap,0,0,SRCCOPY);
dFlag = ::CloseClipboard();

Pointes are moderate.
Thanque,
with love,
Bhavani P Polimetla
Bhavani_73@hotmail.com
Avatar of polimetla
polimetla

ASKER

I visited www.codeguru.com.
But it didn't helped me in any way.

1. Problems with your code

(i) On clipboard CF_DIB is device independant bitmap, you should be looking for CF_BITMAP if you want an HBITMAP

(ii) BitBlt stuff is wrong
- you can't cast pDC to HDC - you pDC->m_hDC
- you can't cast hBitmap to HDC, use
HDC hdcBitmap = ::CreateCompatibleDC( pDC->m_hDC ) ;
::SelectObject( hdcBitmap, hBitmap ) ;
then change "(HDC)hBitmap" to hdcBitmap
- get rid of GetDC.  If you have pDC to draw on the view, you don't need it
- clean up code for bitmap and DC required
DeleteDC( hdcBitmap ) ;
DeleteObject( hBitmap ) ;
- BitBlt scaling params incorrect, you wanna make these match the bitmap size, try
BITMAP bm ;
::GetObject( hBitmap, sizeof bm, &bm ) ;
replace 800 with bm.bmWidth
replace 600 with bm.bmHeight

2. To save to file an HBITMAP

Goto http://www.codeguru.com/bitmap/index.shtml

Use code from here
"Converting DDB to DIB - Zafir Anjum (1998/08/05)" to convert your HBITMAP to DIB

Use code from here
"Writing a bitmap to a BMP file - Zafir Anjum (1998/08/05) " to save your DIB to a file.


3. Remember

DIB = Device Independant Bitmap - identified by a HANDLE
DDB = Device Dependant Bitmap - identified by an HBITMAP

HDC = a device context - the thing you draw to.  - C type is HDC
CDC = MFC wrapper round an HDC. C++ type is CDC

Convert HDC to CDC using
CDC::FromHandle( hDC ) ;

Convert CDC to HDC using
pDC->m_hDC


Dear brother,

after getobject() the values are as follows.
bm.width = 1598196250
bm.height = 1244584
all functions are returning 1.
But I didn't see the bitmap on view
The following code is not printing bitmap on view
I didn't under stood why?
Help me please....

void CCboardView::OnDraw(CDC* pDC)
{

BOOL dFlag;      
HANDLE hBitmap;            
HDC hdcBitmap;
BITMAP bm;
      
dFlag = ::OpenClipboard(m_hWnd);             
hBitmap = ::GetClipboardData(CF_DIB);            
hdcBitmap= ::CreateCompatibleDC(pDC->m_hDC);           
::GetObject(hBitmap, sizeof bm, &bm );
   
dFlag = ::BitBlt(pDC->m_hDC,0,0,bm.bmWidth,bm.bmHeight,hdcBitmap,0,0,SRCCOPY);             

dFlag = ::CloseClipboard();

DeleteDC(hdcBitmap);
DeleteObject(hBitmap);

}

with regards,
Bhavani P Polimetla
ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.

Now One more problem. How to save the bitmap to .BMP file?

with regards,
Bhavani P Polimetla