Recent

Author Topic: BMP printing to PdfCreator but not printer  (Read 4699 times)

Old DOS programmer

  • Newbie
  • Posts: 3
BMP printing to PdfCreator but not printer
« on: July 08, 2015, 12:10:59 am »
I've found the whole business of printing a graphic really confusing.  I've been testing with PdfCreator and it seemed to finally be working perfectly but when I tried printing directly to my HP LaserJet, no graphic appears on the page.  It was testing it  with some text and some line drawing routines and they all worked perfectly but no BMP.  Here's my code (and I have both Printers and PrinterStuff in my uses statement).

Thanks in advance for any ideas/answers.

procedure TReportForm.TestPrintClick(Sender: TObject);
VAR
  TheWidth,TheHeight : INTEGER;
  WidthStr,HeightStr : STRING;
  ExecuteOk : BOOLEAN;
  LogoBitMap : TBitmap;
  Scale : Integer;
  LeftEdge,TopRow : INTEGER;
BEGIN
  ExecuteOk := PrintDialog1.Execute;
  IF ExecuteOk THEN BEGIN
    Printer.BeginDoc;
    TheWidth := Printer.PageWidth;
    TheHeight := Printer.PageHeight;
    Printer.Title := 'Logo Print Test';
    LogoBitMap := TBitMap.create;
    LogoBitMap.LoadFromFile('RogersLogo.bmp');
    Scale := Min(Printer.PageWidth div LogoBitMap.Width,
                 Printer.PageHeight div LogoBitMap.Height);
    LeftEdge := 300;
    TopRow := 600;
    Printer.Canvas.MoveTo(LeftEdge,TopRow);
    Printer.Canvas.StretchDraw(Rect(LeftEdge, TopRow,
                                    LeftEdge+(LogoBitMap.Width*(Scale DIV 10)),
                                    TopRow+(LogoBitMap.Height*(Scale DIV 10))),
                               LogoBitMap);
    Printer.EndDoc;
    LogoBitMap.Free;
  end;
end;

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: BMP printing to PdfCreator but not printer
« Reply #1 on: July 08, 2015, 05:06:44 am »
This prints any BMP just fine on my Samsung laser printer....

If it does not print on your printer, look here:
http://www.scalabium.com/faq/dct0090.htm

Code: [Select]
procedure TForm1.TestPrint;
var
  LogoBitMap: TBitmap;
  Scale:      Integer;
const
  L = 600;
  T = 300;

begin
  if PrintDialog1.Execute then
  begin
    Printer.BeginDoc;
    Printer.Title := 'Logo Print Test';
    try
      LogoBitMap := TBitMap.Create;
      LogoBitMap.LoadFromFile('c:\temp\HB.bmp');
      Scale := Min(Printer.PageWidth div LogoBitMap.Width, Printer.PageHeight div LogoBitMap.Height);
      Printer.Canvas.StretchDraw(Rect(L, T, L + (LogoBitMap.Width  * (Scale div 10)),
                                 T + (LogoBitMap.Height * (Scale div 10))), LogoBitMap);
      Printer.EndDoc;
    finally
      LogoBitMap.Free;
    end;
  end;
end;
« Last Edit: July 08, 2015, 05:35:09 am by kpeters58 »
Lazarus 2.0.4/FPC 3.0.4/Win 64

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: BMP printing to PdfCreator but not printer
« Reply #2 on: July 08, 2015, 09:56:56 am »
Did you find out what your "Scale" is. Showmessage(inttostr(Scale)); just before your StretchDraw.

For example with a bitmap of 686 wide I get a Scale of 7. (PageWidth of 4958 div 686)
After that you do a multiplication by Scale div 10. That a multiplication by 0.

So you're printing an image which you stretch to 0x0.

If you want to stretch the image to page-format you want to loose the "div 10".
(You also forgot to include the "borders" in your scale-calculation.)
Code: [Select]
    LeftEdge := 300; // set these ABOVE the scale calculation
    TopRow := 600;
    Scale := Min((Printer.PageWidth - 2 * LeftEdge) div LogoBitMap.Width,
      (Printer.PageHeight - 2 * TopRow) div LogoBitMap.Height);
    Printer.Canvas.StretchDraw(Rect(LeftEdge, TopRow,
      LeftEdge + (LogoBitMap.Width * Scale),
      TopRow + (LogoBitMap.Height * Scale)), LogoBitMap);

And if you wanted the image 10 times smaller you would need to do the "div 10" over the whole (LogoBitMap.Width * Scale):
Code: [Select]
    LeftEdge := 300; // set these ABOVE the scale calculation
    TopRow := 600;
    Scale := Min((Printer.PageWidth - 2 * LeftEdge) div LogoBitMap.Width,
      (Printer.PageHeight - 2 * TopRow) div LogoBitMap.Height);
    Printer.Canvas.StretchDraw(Rect(LeftEdge, TopRow,
      LeftEdge + (LogoBitMap.Width * Scale) div 10,
      TopRow + (LogoBitMap.Height * Scale) div 10), LogoBitMap);

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: BMP printing to PdfCreator but not printer
« Reply #3 on: July 08, 2015, 10:07:52 am »
guys you do know that the scale used is incorrect right? You have to use the printer.dpi.X and Screen.DPI.X for scale calculations otherwise is not a 1:1 scale is a "fit to page" scale. in that case some auto rotate ability should be also coded in I guess. In any case use the procedure from the scaladium site for stable results.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: BMP printing to PdfCreator but not printer
« Reply #4 on: July 08, 2015, 10:18:28 am »
@taaz (and @kpeters58), it's fine to point to a site with some "stable" code but that code won't run in Lazarus/FPC.

Lazarus/FPC is missing the DIB functions ( GetDIBSizes,  GetDIB, StretchDIBits, etc. )

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: BMP printing to PdfCreator but not printer
« Reply #5 on: July 08, 2015, 10:33:22 am »
@taaz (and @kpeters58), it's fine to point to a site with some "stable" code but that code won't run in Lazarus/FPC.

Lazarus/FPC is missing the DIB functions ( GetDIBSizes,  GetDIB, StretchDIBits, etc. )
missing them or not its the only way that is guaranteed to work. If that means that those calls need to be defined by the end user or install the jedi api (if it has them defined that is) so be it. If the user does not have the problem in windows but in some other hten he has to make it clear so we know what we answering (or not answering) for.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: BMP printing to PdfCreator but not printer
« Reply #6 on: July 08, 2015, 11:39:13 am »
Those functions are in unit Windows. GetDibSizes is not, but on the other hand that is not in the Windows 8.1 SDK either, so either VCL, misspelled, deprecated or something like it.

Old DOS programmer

  • Newbie
  • Posts: 3
Re: BMP printing to PdfCreator but not printer
« Reply #7 on: July 08, 2015, 04:54:00 pm »
Did you find out what your "Scale" is. Showmessage(inttostr(Scale)); just before your StretchDraw.

For example with a bitmap of 686 wide I get a Scale of 7. (PageWidth of 4958 div 686)
After that you do a multiplication by Scale div 10. That a multiplication by 0.

So you're printing an image which you stretch to 0x0.

If you want to stretch the image to page-format you want to loose the "div 10".
(You also forgot to include the "borders" in your scale-calculation.)
Code: [Select]
    LeftEdge := 300; // set these ABOVE the scale calculation
    TopRow := 600;
    Scale := Min((Printer.PageWidth - 2 * LeftEdge) div LogoBitMap.Width,
      (Printer.PageHeight - 2 * TopRow) div LogoBitMap.Height);
    Printer.Canvas.StretchDraw(Rect(LeftEdge, TopRow,
      LeftEdge + (LogoBitMap.Width * Scale),
      TopRow + (LogoBitMap.Height * Scale)), LogoBitMap);

And if you wanted the image 10 times smaller you would need to do the "div 10" over the whole (LogoBitMap.Width * Scale):
Code: [Select]
    LeftEdge := 300; // set these ABOVE the scale calculation
    TopRow := 600;
    Scale := Min((Printer.PageWidth - 2 * LeftEdge) div LogoBitMap.Width,
      (Printer.PageHeight - 2 * TopRow) div LogoBitMap.Height);
    Printer.Canvas.StretchDraw(Rect(LeftEdge, TopRow,
      LeftEdge + (LogoBitMap.Width * Scale) div 10,
      TopRow + (LogoBitMap.Height * Scale) div 10), LogoBitMap);

The Scale is 21 so Scale DIV 10 = 2.  I also tried it without the DIV 10 and got the same result: it printed to OdfCreator (albeit much larger than I wanted) but not to the LaserJet.  And while I didn't calc the borders, I did arbitrarily set the edges to LeftEdge := 300 and TopRow := 600 so it should be in the printable range of the page.

I may be wrong but I don't think the calculations are the issue (however crudely they may have been made).  If they were, then why would the graphic print perfectly on the PdfCreator and not at all on the printer?

 

TinyPortal © 2005-2018