• C

Unable to create color bitmap in Compatible Device Context

/* 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 ;

Who is Participating?
Andy_KeysConnect With a Mentor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.