Solved

Unable to create color bitmap in Compatible Device Context

Posted on 1998-06-16
1
298 Views
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 */
    SelectObject(hdcMem,hBmp);
    SelectObject(hdcMem2,hBmp2);
    /* Initialization of the contents of the Bitmaps */
    PatBlt(hdcMem,0,0,xPage,yPage,WHITENESS);
    PatBlt(hdcMem2,0,0,xPage,yPage,WHITENESS);
    /*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*/
    DrawData(hwnd,hdcMem,TRUE,FALSE);


    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) ;
                 DeleteObject(hBmp);
                 DeleteObject(hBmp2);
                 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) ;
               else
                    bError = TRUE ;
               }
          }
     else
          bError = TRUE ;

     DeleteDC (hdcPrn) ;
     DeleteObject(hBmp);
     DeleteObject(hBmp2);
     return bError ;
     }

0
Comment
Question by:mvylita
1 Comment
 
LVL 1

Accepted Solution

by:
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


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

828 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