Solved

copying color bitmap to clipboard?

Posted on 1998-10-28
8
242 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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