Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copying color bitmap to clipboard?

Posted on 1998-10-28
8
Medium Priority
?
257 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 280 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

824 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