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?