Recent

Author Topic: Printing a Timage picture  (Read 1404 times)

woodguy

  • Newbie
  • Posts: 5
Printing a Timage picture
« on: March 11, 2023, 03:47:39 pm »
I'm posting this in the beginners section - if it should be elsewhere mods can feel free to move it.

I've created an application, one feature of which is the ability to print a preview image displayed in a Timage.  This is the relevant code:

var
    p_rect:trect;
    Scale:      Integer;
    myprinter:tprinter;
  const
    OriginX = 175;
    OriginY = 175;

  begin
    myprinter:=printer;
    Scale := Min(myPrinter.PageWidth div image1.picture.graphic.Width, myPrinter.PageHeight div image1.picture.graphic.Height);

    P_rect:=rect(OriginX, OriginY, OriginX + (image1.picture.graphic.Width  * Scale),OriginY + (image1.picture.graphic.Height * Scale));

    if PrintDialog1.Execute then
    begin
      Myprinter.BeginDoc;

      try
        //myprinter.Canvas.StretchDraw(P_rect,image1.picture.graphic);
        myprinter.Canvas.Draw( originx,originy,image1.picture.graphic);
        Myprinter.EndDoc;
        finally
        myprinter.free ;

      end;
    end;
  end;             

I have 2 printers, both lasers and both network accessible only.  For testing I have used the PDF24 printer driver and printed to a pdf file.  This works - either with draw or stretchdraw and produces the expected results. When attempting to print to one of the lasers however, I get no output. Looking at the printer queue I see a job in the printing state which after a few seconds changes to an error state. This cycles indefinitely.

After executing the print, I am able to use other features of the application normally, but when I exit the application (via application.terminate) I get an error.   In the case of running the application from the IDE, I get the first error message.  If I exit from running the executable directly, I get the second error message. The second message takes some time to appear.

Operating system is windows 11 and is current with all updates.
 
I have looked all over for examples of code to perform this print and have found a few different approaches and tried them all.  Some users have experienced a similar problem and I have seen posts which say that some printers simply cannot be printed to by Lazarus. Some produce no output and some produce erratic output. Some approaches I have tried produce the mysterious "Printer is Printing" error.

I have been working on this printing issue for several days - Does anyone have a better way to print the image - or an explanation for the errors?

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Printing a Timage picture
« Reply #1 on: March 11, 2023, 04:33:32 pm »
myprinter.free is wrong, don't free it.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Printing a Timage picture
« Reply #2 on: March 11, 2023, 04:38:30 pm »
You assign the Printer (of unit printers) to a local variable myprinter, and at the end you destroy this myprinter. I think this is wrong because when you do "myprinter := Printer" you do not create a new instance of TPrinter, you simply copy the pointer. When you thus call "myprinter.Free" you really destroy the global Printer. And when the application terminates the Printers unit attempts to destroy Printer itself - which already had been destroyed by you, and this causes the error.

Another issue is that you access the Printer's properties before "PrintDialog.Execute". When a different printer is selected in the dialog, the PageWidth and PageHeight on which you had based the calculation of the destination image rectangle is no longer valid. Better to move this code after "PrintDialog.Execute".

The following modification of your code words correctly for me:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Math, Printers;
  3.  
  4. function mm2px(mm: Double; DPI: Integer): Integer;
  5. begin
  6.   Result := round(mm/25.4 * DPI);
  7. end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. const
  11.   OriginX_mm = 20;  // in millimeters
  12.   OriginY_mm = 20;  // in millimeters
  13. var
  14.   R: TRect;
  15.   Scale: Double;
  16.   G: TGraphic;
  17.   OriginX, OriginY: Integer;
  18. begin
  19.   G := Image1.Picture.Graphic;
  20.  
  21.   if PrintDialog1.Execute then
  22.   begin
  23.     OriginX := mm2px(OriginX_mm, Printer.XDPI);
  24.     OriginY := mm2px(OriginY_mm, Printer.YDPI);
  25.     Scale := Min((Printer.PageWidth - 2*OriginX)/G.Width, (Printer.PageHeight- 2*OriginY)/G.Height);
  26.     R := Rect(OriginX, OriginY, OriginX + round(G.Width * Scale), OriginY + round(G.Height * Scale));
  27.  
  28.     Printer.BeginDoc;
  29.     try
  30.       Printer.Canvas.StretchDraw(R, G);
  31.       //Printer.Canvas.Draw(OriginX, OriginY, G);
  32.     finally
  33.       Printer.EndDoc;
  34.     end;
  35.   end;
  36. end;

woodguy

  • Newbie
  • Posts: 5
Re: Printing a Timage picture
« Reply #3 on: March 11, 2023, 04:55:28 pm »
Thanks Guys for the super fast replies. The error on exit problem is solved and even better thanks to WP I understand why.

Anyone have an idea how to get the lasers to print?  Their problem remains the same.  Is this one unsolveable?  I must have an old usb connected inkjet somewhere I can hook up and try but that really doesn't solve my problem. Is it the fact that they are network accessed or just those particular printers? One is HP and one Canon.

One again thanks for solving the error problem.

Ken

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Printing a Timage picture
« Reply #4 on: March 11, 2023, 05:23:26 pm »
Sounds like a printer driver issue... Can you try to install a different printer driver? For a similar printer model, or a newer (or older) driver provided by the printer manufactorer, or a driver provided by Windows?

woodguy

  • Newbie
  • Posts: 5
Re: Printing a Timage picture
« Reply #5 on: March 11, 2023, 05:40:20 pm »
Here's an update:  I've been able to get the HP laser to work both as USB connected and network connected, the latter by connecting to the printer as a shared printer from another system on the local network. The printer is usb connected to that system.  As a point of interest, a direct network connection to the HP laser does not work.

As for the canon printer: it is getting on in years and the drivers are old and not likely to be replaced.

Could be that there is a networking issue that affects printing to network connected printers but I'm calling this problem solved.

If there is anyone out there with a network connected printer who can try this, we might learn if there is a network issue.

Thanks for the help. Great forum.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Printing a Timage picture
« Reply #6 on: March 11, 2023, 06:33:44 pm »
You did not mention whether the printing issue is solved in case of the usb-connected HP.

And as for the Canon printer: Even if it's old did you try to look for a driver among those coming with Windows? And even if you can't find the exact model maybe a similar one might work.

woodguy

  • Newbie
  • Posts: 5
Re: Printing a Timage picture
« Reply #7 on: March 11, 2023, 07:31:30 pm »
Can't connect the canon via usb at the moment, but I will try when possible. I will also look for new drivers.

woodguy

  • Newbie
  • Posts: 5
Re: Printing a Timage picture
« Reply #8 on: April 15, 2023, 09:59:17 pm »
Tried the latest drivers and no change. Got a long enough cable and changed over to USB - works fine.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Printing a Timage picture
« Reply #9 on: April 16, 2023, 03:32:21 am »
Permission issues?

 Network devices normally requires the App to have permission to things like that these days.

 Because you can do via another workstation only means you have access to that workstation in general.

 Just a thought...
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018