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

x
?
Solved

How to copy bitmap from clipboard to view?

Posted on 1998-10-14
5
Medium Priority
?
898 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:polimetla
[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
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:polimetla
ID: 1323302
I visited www.codeguru.com.
But it didn't helped me in any way.

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1323303
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


0
 
LVL 1

Author Comment

by:polimetla
ID: 1323304
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
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 120 total points
ID: 1323305
1. Change this GetClipboardData line to :

hBitmap = ::GetClipboardData(CF_BITMAP);

2. Change the hBitmap declaration line to
HBITMAP hBitmap ;

3. Remember a DIB (HANDLE bitmap) is NOT the same as DDB (HBITMAP bitmap).  If you want to convert between the two see the code guru page I previously mentioned.

4. You missed SelectObject after CreateCompatibleDC
SelectObject( hdcBitmap, hBitmap ) ;

5. Additionally you may want to split the OnDraw function:
(i) Make hdcBitmap and hBitmap members of the class.
(ii) in the constructor of the class set these both to NULL
hdcBitmap = NULL ;
hBitmap = NULL ;
(iii) In the destructor of the class add the following

if ( hdcBitmap != NULL ) DeleteDC( hBitmap ) ;
if ( hBitmap != NULL ) DeleteObject( hBitmap ) ;

(iv) When you get the "paste" menu command add this code (add a menu option to the view, and the use class wiz to add a handler function which will be called when the menu is clicked.

if ( hdcBitmap != NULL ) DeleteDC( hBitmap ) ;
if ( hBitmap != NULL ) DeleteObject( hBitmap ) ;
dFlag = ::OpenClipboard(m_hWnd);
hBitmap = ::GetClipboardData(CF_BITMAP);
hdcBitmap= ::CreateCompatibleDC(pDC->m_hDC);      
SelectObject( hdcBitmap, hBitmap ) ;
dFlag = ::CloseClipboard();

(v) In OnDraw just do this

if ( ( hdcBitmap != NULL ) && ( hBitmap != NULL ) )
{
::GetObject(hBitmap, sizeof bm, &bm );
dFlag = ::BitBlt(pDC->m_hDC,0,0,bm.bmWidth,bm.bmHeight,hdcBitmap,0,0,SRCCOPY);
}

Why do this ? - because otherwise you get the clipboard data every time the screen repaints.

0
 
LVL 1

Author Comment

by:polimetla
ID: 1323306
Thank you.

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

with regards,
Bhavani P Polimetla
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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