Desaware Home
Products    Purchase    Publishing    Articles   Support    Company    Contact    
Articles
.NET
COM

 

 

bluebar
Contact Desaware and order today

bluebar
Sign up for Desaware's Newsletter for the latest news and tech tips.

Modifying the VB Programmer's Guide PicPrint example to print 8 bit (256 color), 16 bit or 24 bit color.

The current PicPrint example from chapter 12 of the Visual Basic Programmer's Guide to the Win32 API only prints in 16 colors. A number of people have written us asking how to modify it to print additional colors. Here is a quick summary of the techniques to use.

1. First, fix the known bug in the PicPrint program that causes the device setting to fail on occasion:

In the picprint.bas file, add the following constant declaration:

Public Const PRINTER_ACCESS_USE = &H8

In the OpenDefaultPrinter function of picprint.frm change:

pdefs.DesiredAccess = PRINTER_ACCESS_ADMINISTER
to

pdefs.DesiredAccess = PRINTER_ACCESS_ADMINISTER 
   Or PRINTER_ACCESS_USE

2. In the PicPrint.bas file, change the BITMAPINFO type declaration from:

Type BITMAPINFO
   bmiHeader As BITMAPINFOHEADER
   bmiColors(16) As RGBQUAD
End Type

to:

Type BITMAPINFO
   bmiHeader As BITMAPINFOHEADER
   bmiColors(256) As RGBQUAD
End Type

This allocates enough information in the palette for up to 256 colors. You only need this if you wish to create a 256 color DIB. Note that your best results will be achieved with 24 bit color - more on this later.

3. In the PrintBitmap function of the PicPrint sample program, change the line:

bi.bmiHeader.biBitCount = 4

to:

bi.bmiHeader.biBitCount = 8  ' 256 color DIB

or:

bi.bmiHeader.biBitCount = 24 ' 24 bi color

That's it.

Now, you may think that you need to choose an 8 bit color for a 256 color bitmap and a 24 bit color for a higher color bitmap, but this is not the case.

Consider what we are actually doing. We are obtaining a bitmap handle from the picture property of a control. This bitmap may be monochrome, 4 bit (16 color), 8 bit (256 color), 16 bit (65k color) or 24 bit color. But it is a device dependent bitmap (DDB) - we do not know or care about the internal format of this bitmap.

We depend on GetDIBits to covert this DDB into a DIB with a color depth that we specify. Obviously, to preserve quality it is necessary for the color depth of the DIB to be greater or equal to the color depth of the DDB. Here are some examples of what can happen:

  • 8 bit DDB converted into 1 bit DIB - converts the bitmap to monochrome (looks terrible).
  • 8 bit or 16 bit DDB converted into 4 bit DIB - Does it's best possible conversion while reducing the number of colors in the bitmap to 16. This is what PicPrint currently does.
  • 4 bit DDB converted into 24 bit DIB - Looks fine, but you're wasting space, since you only need a 4 bit DIB to represent this bitmap.
  • 24 bit DDB to 8 bit DIB - Windows reduces the number of colors to 256, using a generic 256 color palette (you may wish to change the palette manually using your own algorithm, since Windows is rather stupid about this sort of thing.

What can you conclude from this? If you wish to use the highest quality color, just create 24 bit DIB's. Or perhaps create 4 bit DIB's for 16 color and 24 bit DIB's for higher values. It is not necessary to create an 8 bit DIB to display an 8 bit DDB.

For notification when new articles are available, sign up for Desaware's Newsletter.

articles
Related Products:
 
Products    Purchase    Articles    Support    Company    Contact
Copyright© 2012 Desaware, Inc. All Rights Reserved.    Privacy Policy