Solved

copying color bitmap to clipboard?

Posted on 1998-10-28
8
249 Views
Last Modified: 2010-04-02
I successfully copy a rectanglular area that the user has "rubberbanded" and paste it to the clipboard but only in black&white - HOW DO I KEEP THE COLOR? (MSVC++5.0)
Within OnLButtonUp I have:
      CDC *windowDC = GetDC();
      CDC memDC;
      CBitmap memBitmap, *oldBitmap;
      memDC.CreateCompatibleDC(&memDC);
      memBitmap.CreateCompatibleBitmap(&memDC, w, h);
      memDC.SelectObject(memBitmap);
      memDC.BitBlt(0, 0, w, h,windowDC,
                rubberBand.left+borderSize,
            rubberBand.top+borderSize, SRCCOPY);
0
Comment
Question by:borghard
[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
  • 5
  • 3
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 1176425
You need to create a compatible bitmap using a display DC, not the memory DC.

details follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176426
Most DCs can handle only a single bitmap format.  For example, a DC for the display (console,screen, what ever you want to call it) can handle bitmaps that use the same color format as the screen.  A DC for a printer can handle only bitmaps for the same color fromat as the printer, ussually monochrome.  But memory DCs are special and can handle a variety of color formats.  When you do

memDC.CreateCompatibleDC(&windowDC);

it creates a memory DC that is capable of being compatible with the screen DC, but it is also capable of being compatible with other DCs  (Mostly it is capable of being compatible with all the common VGA color formats.)  So what?  Well selected inside this memory DC is its inital bitmap.  This is a 1 pixel square MONOCHROME bitmap.  Thus even though you wanted a DC compatible with the screen, it gave you one whose current bitmap is monochrome.  No problem yet, but then you do

memBitmap.CreateCompatibleBitmap(&memDC, w, h)

In other word you create a bitmap that is compatible with the memory DC.  Well the memory DC can work in lots of color formats which one should the new bitmap be in?  Answer the same one as the memory DC's current bitmap.  That bitmap is monochrome so the new bitmap is monochrome and thus it doesn't match the screen format, even though you requested a memory DC  that does.  

The way around this is to create the bitmap using the display DC, not the memory DC.  Then the bitmap will have the same color fromat as the display.

One more thing, when you select the new bitmap into the memory DC it selectes out that 1 pixel bitmap.  You must reselect that 1 pixel bitmap back into the memory DC before your destroy the memory DC.  That is very important.

Let me know if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176427
By the way, once you get this working, you might want to consider a big change.  I believe you would be best off putting a device independant bitmap onto the clipboard instead.  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:borghard
ID: 1176428
Thanks!  Will wait for your details - Bogey.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176429
Those were the details.  Do you have questions?
0
 

Author Comment

by:borghard
ID: 1176430
Ahhhhh.  Will try out first thing in the morning -
Thanks - Bogey
0
 

Author Comment

by:borghard
ID: 1176431
Coooool!  It is now working. Thanks for the well written detailed explanation (I answered your first e-mail before the rest came over).  Hopefully I coded this correctly.  I'm going to try to do this using a DIB sometime in the future, (as I am a novice to C++ ).
Thanks again -
Bogey

FYI, here is the code now:
            CDC *windowDC = GetDC();
            CDC memDC;
            CBitmap memBitmap, *oldBitmap;
                            memDC.CreateCompatibleDC(windowDC);
            memBitmap.CreateCompatibleBitmap(windowDC, w, h);
            memDC.SelectObject(memBitmap);
            memDC.BitBlt(0, 0, w, h, windowDC, rubberBand.left+borderSize,
                              rubberBand.top+borderSize, SRCCOPY);
                        memDC.SelectObject(oldBitmap);
                        ReleaseDC(&memDC);
                        ReleaseDC(windowDC);
                        OpenClipboard();
                        EmptyClipboard();
                        SetClipboardData(CF_BITMAP, memBitmap);
                        CloseClipboard();


0
 
LVL 22

Expert Comment

by:nietod
ID: 1176432
The line

memDC.SelectObject(oldBitmap);

returns a pointer to the CBitmap that was previously selected into the DC.  It is your responsibility to save that pointer and when you are done with the DC, you must select that bitmap back in before you release the DC.  I don't use MFC, but that is the way the regular API works and I don't think that MFC works differently in this respect.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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