problem printing a TImage

Posted on 2001-06-14
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 50 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 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 =:)
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 74
Delphi XE10 Round Image 2 128
enhance the following code 3 32
Firemonkey allowing RTL on android 6 32
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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