Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 =:)
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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