Unable to create color bitmap in Compatible Device Context

Posted on 1998-06-16
Last Modified: 2008-03-17
/* Unable to create color bitmap in Compatible Device Context */
I try to do some manipulation on the drawing right before printing (function BitBlt)
unfortunately when I print, I loose all color informations, It seems that the bitmaps that I create
by calling CreateCompatibleBitmap returns handle to monochrome Bitmaps. I really doesn't know what to do
to create color bitmap */

BOOL PrintSelect180Rotation (HWND hwnd, FPOINT Pt1,FPOINT Pt2 )
     static DOCINFO di     = { sizeof (DOCINFO), "Print1: Printing", NULL } ;
     BOOL           bError = FALSE ;
     HDC            hdcPrn,hdcMem,hdcMem2;
     int            i,j,xPageMem,yPageMem ;
    HPALETTE hPalette;
       PBITMAPINFO pbmi;
       HBITMAP hBmp = NULL;
    HBITMAP hBmp2 = NULL;
    HBITMAP hbtest = NULL;

       char mes[256];
       int  Max_select_y,Max_select_x,select_x,select_y;

    /* Get Printer DC  Presently I'am using HP PaintJet XL 300*/
    if (NULL == (hdcPrn = GetPrinterDC(hwnd)))
          return TRUE ;

    xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
       yPage = GetDeviceCaps (hdcPrn, VERTRES) ;

    /* Create DC temporary using for storing some drawing */
    hdcMem = CreateCompatibleDC(hdcPrn);
    hdcMem2 = CreateCompatibleDC(hdcPrn);
    /* Create Bitmaps to store drawing */
    hBmp = CreateCompatibleBitmap(hdcMem,xPage,yPage);    /*!!!!!<----- Bitmap, I get from  is monochrome bitmap
                                                                                                                      but I need to  create color bitmaps.  */
    hBmp2 = CreateCompatibleBitmap(hdcMem2,xPage,yPage);
    /* Selecting Bitmaps into memory */
    /* Initialization of the contents of the Bitmaps */
    /*Keeping the same map mode */
    SetMapMode(hdcMem, GetMapMode(hdcPrn));

    /* Setting  global data for WM_PAINT */
    xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
       yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
    /* Drawing in the bitmap*/

    pbmi = CreateBitmapInfoStruct(hwnd,hBmp);
    CreateBMPFile(hwnd, "d:\\Image.bmp", pbmi,hBmp, hdcMem) ;
       select_x = Pt1.x *xPage;
       select_y = Pt1.y *yPage;
       Max_select_x =      Pt2.x *xPage - select_x;
       Max_select_y = Pt2.y *yPage - select_y;

          if((GetDeviceCaps(hdcPrn,RASTERCAPS) & RC_BITBLT)==0 || (GetDeviceCaps(hdcMem,RASTERCAPS & RC_BITBLT)==0 ))
            MessageBox(hwnd,"BLTBIT not suported","Impossible a imprimer",0L);
               DeleteDC (hdcPrn) ;
                 return bError ;

      /*        rotating a bitmap by 180 degrees */
       BitBlt(hdcMem2 ,0,0,xPageMem,yPageMem,hdcMem,0,0,SRCCOPY);
            for(i = 0;i<Max_select_y;i++)
                         BitBlt(hdcMem,select_x,select_y+i,Max_select_x,1,hdcMem2,select_x,select_y + Max_select_y -i,SRCCOPY);

              BitBlt(hdcMem2 ,0,0,xPageMem,yPageMem,hdcMem,0,0,SRCCOPY);
            for(i = 0;i<Max_select_x;i++)
                   BitBlt(hdcMem,select_x+i,select_y,1,Max_select_y,hdcMem2,select_x + Max_select_x -i,select_y ,SRCCOPY);

    /* Printing */
     if (StartDoc (hdcPrn, &di) > 0)
          if (StartPage (hdcPrn) > 0)
               xPage = GetDeviceCaps (hdcPrn, HORZRES) ;
                               yPage = GetDeviceCaps (hdcPrn, VERTRES) ;
               xPageMem = GetDeviceCaps (hdcMem, HORZRES) ;
                           yPageMem = GetDeviceCaps (hdcMem, VERTRES) ;
                              StretchBlt(hdcPrn,0,0,xPage,yPage,hdcMem,0,0,xPageMem,yPageMem, SRCCOPY);
               if (EndPage (hdcPrn) > 0)
                    EndDoc (hdcPrn) ;
                    bError = TRUE ;
          bError = TRUE ;

     DeleteDC (hdcPrn) ;
     return bError ;

Question by:mvylita
1 Comment

Accepted Solution

Andy_Keys earned 100 total points
ID: 1251236
When a dc is created its bitmap by default is a 1*1 monochrome bitmap. Create a bitmap by doing something like
CreateBitmap( Width, Height, MemDC.GetDeviceCaps( PLANES ), MemDC.GetDeviceCaps( BITSPIXEL ), NULL ); instead of CreateCompatibleBitmap this should be a colour bitmap.  NOTE as the DC is a printer DC just make sure that the calls to Get DeviceCaps return sensible values ie one of them should not be one else you still get a monochrome bitmap


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now