problem printing a TImage

Posted on 2001-06-14
Medium Priority
Last Modified: 2010-04-06

i've been trying to send a TImage to my printer, but all i get is a blance piece of paper.

i tried printer.canvas.draw(0,0,image1.picture.graphic), but it doesnt work

printer.canvas := image1.canvas doesnt work eiter, because TCanvas is in this case a read-only value.

does anyone know the solution?
thanks in advance
Question by:brutebass
  • 3
  • 2

Accepted Solution

Colin_Dawson earned 200 total points
ID: 6190179
ah ha, This old chestnut again.  You're trying to send a Device Dependent Bitmap to the printer.  You need to convert the bitmap to a Device Independant Bitmap before you print it.

Check out the Borland website for an example of how to do this.  (I got a procedure from there a couple of years ago, but can't remember where I've put it.)

Colin Dawson

Author Comment

ID: 6190381
ah few years ago..well borland.com has changed over the years, i've looked everywhere, but i can't find it.

isnt there another site with some free components?

Expert Comment

ID: 6190647
Could you try something simple like:

Printer.Canvas.StretchDraw(RECT(0, 0, Printer.PageWidth, Printer.PageHeight), image1.Picture.Bitmap);

Maybe try creating a TBitmap and copy the TImage into it (and then print the TBitmap)

You've probably already tried it but what the hell, that's my two cents of useless rubbish

The Neil =:)
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.


Author Comment

ID: 6190920
Printer.Orientation := polandscape;{PoPortrait;}

  with form5.Image1.Picture.Bitmap do
       { Call GetDIBSizes to get the Info header and image size }
       GetDIBSizes(Handle, InfoSize, ImageSize);
       { Allocate memory for the info header }
       GetMem(info, infosize);
    //Info := MemAlloc(InfoSize);
      { Allocate memory for the Image }
        GetMem(Image, imagesize);
     // Image := MemAlloc(ImageSize);
        { Retrieve the bitmap bits in device-independent format, the
          palette and the Info header }
        GetDIB(Handle, Palette, Info^, Image^);
        with Info^.bmiHeader do begin
        { Call StretchDIBits to print the bitmap }
            StretchDIBits(Printer.Canvas.Handle, 0, 0,
              Printer.PageHeight, 0, 0, biWidth, biHeight, Image, Info^,
              DIB_RGB_COLORS, SRCCOPY);
        FreeMem(Image, ImageSize); // Free the memory allocated
      FreeMem(Info, InfoSize);
      Printer.Orientation := PoPortrait;


Author Comment

ID: 6190932
thanks for helping me on the right way... DBB -> DIB :)
with your help i found some stuff i didnt know about Device Independent Bitmaps. i found some sourcecode on google.com and it works. I submitted the code to the thread on EE.



Expert Comment

ID: 6193421
Glad I could point you in the right direction.  I would have put the code snippet straight in here, but I could not remember where I'd put it.   I've got a really easy method for doing this conversion, wrapped up as a single function call!  Here's the source.

You should find that is is REALLY simple to use. And it also means that you don't have to worry about the conversion.

Thanks for the points.

procedure BltTBitmapAsDib(DestDc : hdc;   {Handle of where to blt}
                          x : word;       {Bit at x}
                          y : word;       {Blt at y}
                          Width : word;   {Width to stretch}
                          Height : word;  {Height to stretch}
                          bm : TBitmap);  {the TBitmap to Blt}
  OriginalWidth :LongInt;               {width of BM}
  dc : hdc;                             {screen dc}
  IsPaletteDevice : bool;               {if the device uses palettes}
  IsDestPaletteDevice : bool;           {if the device uses palettes}
  BitmapInfoSize : integer;             {sizeof the bitmapinfoheader}
  lpBitmapInfo : PBitmapInfo;           {the bitmap info header}
  hBm : hBitmap;                        {handle to the bitmap}
  hPal : hPalette;                      {handle to the palette}
  OldPal : hPalette;                    {temp palette}
  hBits : THandle;                      {handle to the DIB bits}
  pBits : pointer;                      {pointer to the DIB bits}
  lPPalEntriesArray : PPalEntriesArray; {palette entry array}
  NumPalEntries : integer;              {number of palette entries}
  i : integer;                          {looping variable}
  {If range checking is on - lets turn it off for now}
  {we will remember if range checking was on by defining}
  {a define called CKRANGE if range checking is on.}
  {We do this to access array members past the arrays}
  {defined index range without causing a range check}
  {error at runtime. To satisfy the compiler, we must}
  {also access the indexes with a variable. ie: if we}
  {have an array defined as a: array[0..0] of byte,}
  {and an integer i, we can now access a[3] by setting}
  {i := 3; and then accessing a[i] without error}
  {$IFOPT R+}
  OriginalWidth := bm.Width; {Save the original width of the bitmap}
 {Get the screen's dc to use since memory dc's are not reliable}
  dc := GetDc(0);
  IsPaletteDevice := GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE; {Are we a palette device?}
{  dc :=} ReleaseDc(0, dc); {Give back the screen dc}
 {Allocate the BitmapInfo structure}
  if IsPaletteDevice
  then BitmapInfoSize := sizeof(TBitmapInfo) + (sizeof(TRGBQUAD) * 255)
  else BitmapInfoSize := sizeof(TBitmapInfo);
  GetMem(lpBitmapInfo, BitmapInfoSize); {Zero out the BitmapInfo structure}
  FillChar(lpBitmapInfo^, BitmapInfoSize, #0);
  {Fill in the BitmapInfo structure}
  lpBitmapInfo^.bmiHeader.biSize := sizeof(TBitmapInfoHeader);
  lpBitmapInfo^.bmiHeader.biWidth := OriginalWidth;
  lpBitmapInfo^.bmiHeader.biHeight := bm.Height;
  lpBitmapInfo^.bmiHeader.biPlanes := 1;
  if IsPaletteDevice
  then lpBitmapInfo^.bmiHeader.biBitCount := 8
  else lpBitmapInfo^.bmiHeader.biBitCount := 24;
  lpBitmapInfo^.bmiHeader.biCompression := BI_RGB;
  lpBitmapInfo^.bmiHeader.biSizeImage := ( ( lpBitmapInfo^.bmiHeader.biWidth *
                                          longint( lpBitmapInfo^.bmiHeader.biBitCount ) ) div 8 ) *
  lpBitmapInfo^.bmiHeader.biXPelsPerMeter := 0;
  lpBitmapInfo^.bmiHeader.biYPelsPerMeter := 0;
  if IsPaletteDevice
  then begin
         lpBitmapInfo^.bmiHeader.biClrUsed := 256;
         lpBitmapInfo^.bmiHeader.biClrImportant := 256;
  else begin
         lpBitmapInfo^.bmiHeader.biClrUsed := 0;
         lpBitmapInfo^.bmiHeader.biClrImportant := 0;
  {Take ownership of the bitmap handle and palette}
  hBm := bm.ReleaseHandle;
  hPal := bm.ReleasePalette;
 {Get the screen's dc to use since memory dc's are not reliable}
  dc := GetDc( 0 );
  if IsPaletteDevice
  then begin
         {If we are using a palette, it must be}
         {selected into the dc during the conversion}
          OldPal := SelectPalette(dc, hPal, TRUE);
          RealizePalette(dc); {Realize the palette}
  Else OldPal := 0;
  {Tell GetDiBits to fill in the rest of the bitmap info structure}
  GetDiBits( dc, hBm, 0, lpBitmapInfo^.bmiHeader.biHeight, nil, TBitmapInfo(lpBitmapInfo^), DIB_RGB_COLORS );
  {Allocate memory for the Bits}
  hBits := GlobalAlloc( GMEM_MOVEABLE, lpBitmapInfo^.bmiHeader.biSizeImage );
  pBits := GlobalLock( hBits );
  GetDiBits( dc, hBm, 0, lpBitmapInfo^.bmiHeader.biHeight, pBits, TBitmapInfo( lpBitmapInfo^ ), DIB_RGB_COLORS); {Get the bits}
  if IsPaletteDevice then
    {Lets fix up the color table for buggy video drivers}
    GetMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
    {$IFDEF VER100}
    NumPalEntries := GetPaletteEntries( hPal, 0, 256, lPPalEntriesArray^ );
    NumPalEntries := GetSystemPaletteEntries( dc, 0, 256, lPPalEntriesArray^ );
    for i := 0 to (NumPalEntries - 1) do
      lpBitmapInfo^.bmiColors[i].rgbRed := lPPalEntriesArray^[i].peRed;
      lpBitmapInfo^.bmiColors[i].rgbGreen := lPPalEntriesArray^[i].peGreen;
      lpBitmapInfo^.bmiColors[i].rgbBlue := lPPalEntriesArray^[i].peBlue;
    FreeMem(lPPalEntriesArray, sizeof(TPaletteEntry) * 256);
  if IsPaletteDevice then
    SelectPalette(dc, OldPal, TRUE); {Select the old palette back in}
    RealizePalette(dc); {Realize the old palette}
{  dc := }ReleaseDc(0, dc); {Give back the screen dc}

  IsDestPaletteDevice := GetDeviceCaps(DestDc, RASTERCAPS) and RC_PALETTE = RC_PALETTE; {Is the Dest dc a palette device?}
  if IsPaletteDevice then
   {If we are using a palette, it must be}
   {selected into the dc during the conversion}
    OldPal := SelectPalette(DestDc, hPal, TRUE);
    RealizePalette(DestDc); {Realize the palette}
  end; {Do the blt}
  StretchDiBits( DestDc, x, y, Width, Height, 0, 0, OriginalWidth, lpBitmapInfo^.bmiHeader.biHeight,
                 pBits, lpBitmapInfo^, DIB_RGB_COLORS, SrcCopy );
  if IsDestPaletteDevice then
    SelectPalette( DestDc, OldPal, TRUE );     {Select the old palette back in}
    RealizePalette( DestDc );    {Realize the old palette}
  end; {De-Allocate the Dib Bits}
  GlobalUnLock(hBits);  GlobalFree(hBits); {De-Allocate the BitmapInfo}
  FreeMem(lpBitmapInfo, BitmapInfoSize);
  {Set the ownership of the bimap handles back to the bitmap}
  bm.Handle := hBm;
  bm.Palette := hPal;
  {Turn range checking back on if it was on when we started}

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

627 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