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
Solved

copying color bitmap to clipboard?

Posted on 1998-10-28
8
244 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
Socket Programming (Unix) 8 141
How to convert MFC APP to Win32 APP. 19 93
gdb doesn't stop on breakpoint 2 93
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…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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