Recent

Author Topic: Print TmapView  (Read 904 times)

cpalx

  • Hero Member
  • *****
  • Posts: 754
Print TmapView
« on: May 02, 2022, 05:58:52 pm »
hello,

does somebody know how toi ptint a map  with TmapView?


Lazarus 2.2 (windows and Linux)

wp

  • Hero Member
  • *****
  • Posts: 13491
Re: Print TmapView
« Reply #1 on: May 03, 2022, 01:48:08 pm »
Use the method PaintToCanvas of the MapView's drawing engine in order to paint the currently visible map to a bitmap. Then apply standard techniques to stretch-draw this bitmap to the printer's Canvas:

Code: Pascal  [Select][+][-]
  1. uses
  2.   ... printers, OSPrinters;
  3.  
  4. procedure TMainForm.Button3Click(Sender: TObject);
  5. const
  6.   LEFT_MARGIN = 10.0;  // mm
  7.   TOP_MARGIN = 10.0;   // mm
  8. var
  9.   bmp: TBitmap;
  10.   PrintedRect: TRect;
  11. begin
  12.   if PrintDialog1.Execute then
  13.   begin
  14.     Printer.Orientation := poLandscape;
  15.     Printer.BeginDoc;
  16.     try
  17.       // Catch the displayed map as a bitmap.
  18.       bmp := TBitmap.Create;
  19.       try
  20.         bmp.SetSize(MapView.Width, MapView.Height);
  21.         MapView.DrawingEngine.PaintToCanvas(bmp.Canvas);
  22.  
  23.         // Calculate output rectangle for image. It must be scaled by the
  24.         // ratio of printer-to-screen pixels per inch.
  25.         PrintedRect := Rect(0, 0,
  26.           MapView.Width * Printer.XDPI div ScreenInfo.PixelsPerInchX,
  27.           MapView.Height * Printer.YDPI div ScreenInfo.PixelsPerInchY
  28.         );
  29.        
  30.         // Move output rectangle to desired position, here: in the top/left corner
  31.         OffsetRect(PrintedRect,
  32.           round(Printer.XDPI/25.4 * LEFT_MARGIN),
  33.           round(Printer.YDPI/25.4 * TOP_MARGIN)
  34.         );
  35.        
  36.         // Stretch-draw the map image to the output rectangle on the printer.
  37.         Printer.Canvas.StretchDraw(PrintedRect, bmp);
  38.       finally
  39.         bmp.Free;
  40.       end;
  41.        
  42.     finally
  43.       Printer.EndDoc;
  44.     end;
  45.   end;
  46. end;

I extended the "fulldemo" of the LazMapViewer package by a bit more advanced code in which the map is scaled such that if fills the width (height) of the printed page (with margins)
« Last Edit: May 03, 2022, 05:14:54 pm by wp »

 

TinyPortal © 2005-2018