Board index » delphi » Print Graphics - Why doesn't this work?

Print Graphics - Why doesn't this work?

Help :)

Why doesn't this work, I am doing it according to the example in
Delphi 3 help which says just copy the graphic to the printers canvas
to print it:

with Printer do
begin
  BeginDoc;
  Canvas.Draw(0,0,ImageActo.Picture.Graphic);
  EndDoc;
end;

All I get is a blank page. I have tried copying to and from another
TImage.Canvas with CopyRect and Draw and it works fine, but when I
copy to and from the TPrinter.Canvas anything I try to put on there
gets lost. If I draw directly on the Printers Canvas (with Line etc.)
something does come out of the printer but it's a right mess. Any
hints/help would be appreciated, esp. thru E-mail (dont reply to the
nospam address) The printer is a HP Laserjet btw.

Cheers, Michel

---8<---
Michel A. de Bree [mailto:M.A.de_B...@Physiology.Medfac.LeidenUniv.nl]
Lab. Fysiologie kamer C506, tel. (071527)6807

 

Re:Print Graphics - Why doesn't this work?


Michel A. de Bree wrote in message <351270de.4240...@news.leidenuniv.nl>...

Quote
>Why doesn't this work, I am doing it according to the example in
>Delphi 3 help which says just copy the graphic to the printers canvas
>to print it:

I have a Printer Demo program on the Delphi and Other Projects
page of my Computer Lab that shows how to print graphics in
Delphi 3 using StretchDIBits.

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
MedTech Research Corporation, Lenexa, KS  USA

Re:Print Graphics - Why doesn't this work?


Michel,
  There are known problems when using Draw and StretchDraw to send things
to the printer.  On some printers nothing is printed or intermittently
nothing is printed.  Instead try the following code which may also fix your
problem (it works in D1 and D2. Needs modification to work in D3 which is
shown after the D1/D2 version):

****** Delphi 1 and Delphi 2 version ******

procedure MemFree(Ptr: Pointer; Size: Longint);
begin
  if Size < 65535 then
    FreeMem(Ptr, Size)
  else
    GlobalFreePtr(Ptr);
end;

  {Prints BitMap }
procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap: TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize: Integer;
  {$IfDef Win32}                   {Checks if using Delphi 2.0}
  BitsSize : Integer;
  {$Else}
  BitsSize : LongInt;
  {$EndIf}
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
  Header := MemAlloc(HeaderSize);
  Bits := MemAlloc(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
        DestRect.Right - DestRect.Left, DestRect.Bottom - DestRect.Top,
        0, 0, ABitmap.Width, ABitmap.Height, Bits, TBitmapInfo(Header^),
        DIB_RGB_COLORS, SRCCOPY);
  finally
    MemFree(Header, HeaderSize);
    MemFree(Bits, BitsSize);
  end;
end;

To call it use something like:

DrawImage(Printer.Canvas, Rect(Left, Top, Right, Bottom), MyBitmap);

  This is similar to the code which is provided in the Manuals.txt file
(it's at the end of the txt file) that is installed in the Delphi directory
of D1.

****** Delphi 3 version (I haven't tested this but have been told it works)
******

procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap: TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize, BitsSize: Integer;
begin
        GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
        GetMem(Header, HeaderSize);
        GetMem(Bits, BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
        DestRect.Right - DestRect.Left, DestRect.Bottom - DestRect.Top,
        0, 0, ABitmap.Width, ABitmap.Height, Bits, TBitmapInfo(Header^),
        DIB_RGB_COLORS, SRCCOPY);
  finally
    FreeMem(Header, HeaderSize);
    FreeMem(Bits, BitsSize);
  end;
end;

To call it use something like:

DrawImage(Printer.Canvas, Rect(Left, Top, Right, Bottom), MyBitmap);

Of course in either case you will heve to scale the values of Rect(Left,
Top, Right, Bottom) according to the DPI setting of the printer.

Hope this helps!
--

Rodney E Geraghty
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Michel A. de Bree <nos...@nospam.nospam.no> wrote in article
<351270de.4240...@news.leidenuniv.nl>...

Quote
> Help :)

> Why doesn't this work, I am doing it according to the example in
> Delphi 3 help which says just copy the graphic to the printers canvas
> to print it:

> with Printer do
> begin
>   BeginDoc;
>   Canvas.Draw(0,0,ImageActo.Picture.Graphic);
>   EndDoc;
> end;

> All I get is a blank page. I have tried copying to and from another
> TImage.Canvas with CopyRect and Draw and it works fine, but when I
> copy to and from the TPrinter.Canvas anything I try to put on there
> gets lost. If I draw directly on the Printers Canvas (with Line etc.)
> something does come out of the printer but it's a right mess. Any
> hints/help would be appreciated, esp. thru E-mail (dont reply to the
> nospam address) The printer is a HP Laserjet btw.

> Cheers, Michel

> ---8<---
> Michel A. de Bree [mailto:M.A.de_B...@Physiology.Medfac.LeidenUniv.nl]
> Lab. Fysiologie kamer C506, tel. (071527)6807

Other Threads