Solved

Unable to create color bitmap in Compatible Device Context

Posted on 1998-06-16
1
295 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which version of C should I use when Perl is too slow? 13 181
convert char array to number in c 5 85
gdb doesn't stop on breakpoint 2 67
Why isn't object file created? 6 57
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…
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…
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 conditional statements in the C programming language.

813 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

12 Experts available now in Live!

Get 1:1 Help Now