Recent

Author Topic: Help with Directions on printing Graphics with text  (Read 13974 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Help with Directions on printing Graphics with text
« on: September 23, 2010, 08:18:13 pm »
I'm at a point in the process where I have to print an image file with a text overlay and I'm a bit bewildered where I'm to start??  %)  Is there a tutorial somewhere on printing graphics and text??  I've seen stuff on the web for Delphi but most of it seems to assume you already have an image in an object already and then show you how to print that.  I need to read in a file and then use it as the background for the text that the user entered like a form on a dot matrix printer.

Pointers???? 

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #1 on: September 23, 2010, 09:18:18 pm »
AFAIK the easiest way is to create a child form that has A Tlable or something you like for your text and add the Timage in the position you want.  
drop a tprinterdialog on your main and call it's .execute and that will select the printer. you do not need to save the results but you may cancel the printout if
.execute = false

Show the form and use the following code
Code: [Select]
const margin = 0.5;

// set the "how much" of the paper to use
procedure SetPrntRect(var rect:TRect);
var edgeX,edgeY:longint;
    f:double;
begin
  f := printer.XDPI * margin;
  edgeX := trunc(f);
  f := printer.YDPI * margin;
  edgeY := trunc(f);

  rect.Top := edgeY;
  rect.Left:= edgeX ;
 // to fill the page
    rect.Right:= Printer.PageWidth - (edgeX*1);  //self.Width*5;
    rect.Bottom:=Printer.PageWidth - (edgeY*1); //self.Height*5;
end;

procedure PrintForm1(frm:TForm); // does the same as GetDC but cleaner
var bitmap:TBitmap;    // tprinter
    R:TRect;
begin
  Application.ProcessMessages;
  SetPrntRect(R);
  bitmap := TBitmap.Create;
  try                     //Application.MainForm
    Bitmap.LoadFromDevice(frm.Canvas.Handle);
    printer.BeginDoc;
    Printer.Canvas.StretchDraw(R, bitmap); //Bitmap.Canvas.Handle;
    Printer.EndDoc;
  finally
    bitmap.Free;
  end;
end;

this may not be the best but it's the fastest time to market and I know this works.

ps: the titel bar is not printed so don't worry about it.
 
« Last Edit: September 23, 2010, 09:29:34 pm by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #2 on: September 23, 2010, 09:33:23 pm »
I forgot some basic code and comments:
{ you will need to add Printer4Lazarus to the project by
    Project->Project Inspector
    click +
      in the "new requirement" tab select Printer4Lazarus in the drop down
      list next to Package Name:
    click OK
  Printer4Lazarus and LCL should now be listed under required packages
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,Forms,Graphics;

procedure PrintForm1(frm:TForm);

implementation
 uses
  Printer4Lazarus // If you get an error on this line then follow direction above.
 ,Printers
 ,LCLIntf     // for GetDC
 ,LCLType     // for HDC def
 ;

const margin = 0.5;...
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #3 on: September 24, 2010, 03:11:33 pm »
I LIKE IT!!!  This will give me a starting point, which was what I was looking for  ;D

Two questions, both of which I might figure out on my own but I'll ask just in case I get stymied.  Since I don't want the form to be displayed can I hide it and still print and the other is can I set the background of the form to an image or will I have to add a timage that covers the entire form?? 

Thanks

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #4 on: September 24, 2010, 04:38:16 pm »
I've answered my own question through trial and error, no you can't print a hidden form and no you can't set a background, at least I couldn't find a form option for it. 

The code did give me a jumping off point and I can now load in a jpeg from a file and print it any size I want  :)

I just have to figure out how to add text to the canvas.  It's going to be harder this way but I wont have to depend on a form showing to print.

One other question, currently I'm reading in scanned in images, jpg format around 2 meg, to put on the canvas, is there any way to read in the image and convert it to something I can save as part of the program???  I'd like the final program to contain everything but the databases and the ini file so that the computer illiterate people I'm writing it for will not have the chance to accidently 'misplace'  ::) the invoice forms.  I can hear the phone ringing now with someone complaining the printouts don't look right. 

End User: 'The invoices don't looke like they should.'
Me: 'What do you mean?'
End User: 'Our logo and headers are missing.'
Me: 'You mean the invoice form is missing but the invoice itself is there?.'
End User: 'Uhh, maybe.....'
Me: 'Did you delete any files recently or try to 'Clean up' your PC??'
End User: 'Absolutely Not!, I never touch the PC.'
Me: 'Let me jump on your system remotely....Yep missing the jpeg files of the invoice forms.'
End User: 'How'd that happen??'
Me: 'Squirrels I guess, since you told me you didn't touch the PC..............'

You know my life would be much easier without end users  ;D



 

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #5 on: September 24, 2010, 06:20:39 pm »
to keep in the exe...
I just drop a few TImage (s) on to my form, go into the picture property and load the default images from files.  I then set the visible property to false.  this way the images are in the .exe

at run time I can make as many copies of the image as I need with pic.Picture:=defualtImg.Picture;

you can shrink the base TImages to be small as a TTimer on the form at design time and it still keeps it's original res for the copies.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #6 on: September 24, 2010, 06:31:26 pm »
Quote
the chance to accidently 'misplace'   the invoice forms. 
I have not needed it yet but there is a LazReport component that people use for text base "invoices" and data base reports.  That may be easier than the graphic print I showed you but I don't know if it will print both without some low level canvas stuff. 

since you do not wish to show your printout, you may need to use the canvas approach anyway or...
locate the form off screen while it's being built ???
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #7 on: September 27, 2010, 02:14:24 pm »
I like the use of the form but I'd like to keep the form and the content together.  Is there anyway I can define a class object that incorporates a form or conversely a form that has a class??  I've tried doing it by creating a form with the timage on it then adding a class to the form but I'm doing something wrong or it can't be done.  :(  What I'd like is to be able to add the form/class to the uses clause and then create an instance of the class that will interact with the hidden form putting the info into the tlabels I've placed in the appropriate locations on the timage and then returning a canvas that I can then print out.  I think I can do it just using a form and then creating a class in a separate file but for consistency sake I'd like to keep the form and class together as one object.

Ideas or directions??

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #8 on: September 27, 2010, 07:38:43 pm »
Look into frames. They are coded so each takes it's own unit by default and are a good way to breakup larger form code.  In your case you can create a few different ones for different printouts.  You can add procedures and property code just like a "role your own" class.  There is no reason a frame can not take up the full form. The frame as a align property just like a panel.

Code: [Select]
  ...
   RF_Frame_U1.SetIdString( DefaultNetwork[i].id);
   RF_Frame_U1.SetFreqTable( DefaultNetwork[i].freqs);

Due to some strange delay/behavior during design time, I don't add frames to my form until the formcreate().  here is the code I use.

Code: Pascal  [Select][+][-]
  1. procedure TfrmTConfig.FormCreate(Sender: TObject);
  2. var i:integer;
  3. begin
  4.     RF_Frame_U1 := TRF_Frame.Create(panalU1_RF); // create the frame and tell the parent it has a kid
  5.     RF_Frame_U1.Parent := panalU1_RF;           // tell the kid who's it's parent is
  6.     RF_Frame_U1.NodeNumber := 1;                            // --- set the frames property so it's automatic "results" are unique
  7.     RF_Frame_U1.OnShowMsg  := @ShowMsg;                 // where to direct any error messages
  8.    // create a 2nd frame for unit 2 and place it on the 2nd panel
  9.     RF_Frame_U2 := TRF_Frame.Create(panalU2_RF); // create the frame and tell the parent it has a kid
  10.     RF_Frame_U2.Parent := panalU2_RF;
  11.     RF_Frame_U2.NodeNumber := 2;
  12.     RF_Frame_U2.OnShowMsg  := @ShowMsg;
  13. end;   
  14.  

By giving the frames a parent, the .free is done by it automatically. AFAIK the display update function is keyed to the forms and they, in turn update their children (simple speak). I'm not sure you can create a form at run-time without this link to parent.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #9 on: September 27, 2010, 07:58:04 pm »
If you didn't see it yet, this thread talks about TImages some more.
http://lazarus.freepascal.org/index.php/topic,10224.msg51322/topicseen.html#new
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #10 on: September 27, 2010, 10:26:37 pm »
I had a brief look at frames and haven't decided if I can use them yet but I do have a question about canvases.  I created a form with a timage on it and then added a class to it that has a function that returns the canvas object of the timage, I then create an instance of the class on the main form and try to fill the bitmap object with the canvas from the class which doesn't seem to work, I get a blank page.  What exactly IS the canvas object of a timage???  and why can't I just pass that to fill in a bitmap???   The original code you showed me, which worked wonderfully, uses a command, 'Bitmap.LoadFromDevice(frm.Canvas.Handle);' to load the bitmap with the canvas but if I try to load it from a canvas object using the Handle I get an error during runtime and if I set the bitmap.canvas := myCanvas I get empty sheet.

 %) %) %) %) %)

What simple explanation am I missing??

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: Help with Directions on printing Graphics with text
« Reply #11 on: September 27, 2010, 11:00:35 pm »
I created a form with a timage on it and then added a class to it that has a function that returns the canvas object of the timage, I then create an instance of the class on the main form and try to fill the bitmap object with the canvas from the class which doesn't seem to work, I get a blank page.
to copy the bitmap of my default TImage(s) to the ones shown on the form, I use the
.picture
ie pic.Picture := DefImgO.Picture;

for my game (tic tac toe), I have a form with a Tpanel.  The Tpanel has 9 Groupbox and each GroupBox has a TImage. on clicking I check to see if there is already an X or O and then call one of these
Code: [Select]
procedure TFMain.PlaceX(Sender:TObject);
var    pic:TImage;
begin
  pic := TPanSquare(sender);
  pic.Picture:= imgX.Picture;
  pic.Tag:=2;
end;

procedure TFMain.PlaceO(Sender:TObject);
var    pic:TPanSquare;
begin
  pic := TPanSquare(sender);
  pic.Picture:= imgO.Picture;
  pic.Tag:=1;
  end;
each TImage's OnClick calls the same "SquareClik" code that (striped down) makes the calls
Code: [Select]
procedure TFMain.SquareClick(Sender: TObject); // sender is the image
begin
  if SquareUsed(Sender) then
     exit;
  if singlePlayer then
       PlayCPU(Sender)
  else PlayTwoPlayer(Sender);
end;

procedure TFMain.PlayTwoPlayer( Sender: TObject);
begin
  if odd(turn) then  begin
     PlaceO(Sender);
     ShowMsg('X turn');
...

and to erase the squares I just loop Thu a list of pointers 

 
Code: [Select]
for i:=low(grid) to high(grid) do begin
    grid[i].Picture:= nil;
    grid[i].Tag:=0;
 end;
 

 The grid var is an array [1..9] of TImage
 and is init at start up as
 
Code: [Select]
   Grid[1] := Image1;    // assign an image to each panel
   Grid[2] := Image2;   
   ...
 

so simple I had the games' GUI working in a few hours. (it was a learning task which is why it took so long :)

I thing I was also trying to use the canvas to start out with but this is what worked in the end.

edited:
I forgot to say on top, I have
type
  TPanSquare = TImage;

so I can change the def later if needed
« Last Edit: September 28, 2010, 04:33:18 am by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #12 on: September 28, 2010, 02:35:05 pm »
The only problem with using the picture object is that it doesn't contain the labels I place on the form.  I think I need to use the canvas object but I must be using it wrong or loading the bitmap object incorrectly.  Any ideas??

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Help with Directions on printing Graphics with text
« Reply #13 on: September 28, 2010, 04:59:27 pm »
My problems now seem to revolve around creating a graphic from a picture included in the program when compiled so I'm going to start a new post on this.  At least now I understand, at least a little bit, how to print an image.  Now I just have to figure out how to create it  :)

 

TinyPortal © 2005-2018