1
/******************************************************************************
2
* $Id: drawmapDirectPrint.cs 10743 2010-11-19 17:05:38Z tbonfort $
5
* Purpose: A C# based mapscript example to draw the map directly onto a GDI
6
* printing device context.
7
* Author: Tamas Szekeres, szekerest@gmail.com
9
******************************************************************************
10
* Copyright (c) 1996-2008 Regents of the University of Minnesota.
12
* Permission is hereby granted, free of charge, to any person obtaining a
13
* copy of this software and associated documentation files (the "Software"),
14
* to deal in the Software without restriction, including without limitation
15
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
* and/or sell copies of the Software, and to permit persons to whom the
17
* Software is furnished to do so, subject to the following conditions:
19
* The above copyright notice and this permission notice shall be included in
20
* all copies of this Software or works derived from this Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28
* DEALINGS IN THE SOFTWARE.
29
*****************************************************************************/
33
using System.Drawing.Printing;
34
using OSGeo.MapServer;
37
/// A C# based mapscript example to draw the map directly onto a GDI printing device context.
41
public static void usage()
43
Console.WriteLine("usage: DrawMapDirectPrint {mapfile} {printername}");
44
System.Environment.Exit(-1);
49
public static void Main(string[] args)
51
Console.WriteLine("");
52
if (args.Length < 2) usage();
54
map = new mapObj(args[0]);
56
Console.WriteLine("# Map layers " + map.numlayers + "; Map name = " + map.name);
57
for (int i = 0; i < map.numlayers; i++)
59
Console.WriteLine("Layer [" + i + "] name: " + map.getLayer(i).name);
64
PrintDocument doc = new PrintDocument();
66
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
68
// Specify the printer to use.
69
doc.PrinterSettings.PrinterName = args[1];
75
Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
77
"\nHelpLink ---\n{0}", ex.HelpLink );
78
Console.WriteLine( "\nSource ---\n{0}", ex.Source );
80
"\nStackTrace ---\n{0}", ex.StackTrace );
82
"\nTargetSite ---\n{0}", ex.TargetSite ); }
85
static void doc_PrintPage(object sender, PrintPageEventArgs e)
87
// Create the output format
88
outputFormatObj of = new outputFormatObj("CAIRO/WINGDIPRINT", "cairowinGDIPrint");
89
map.appendOutputFormat(of);
90
map.selectOutputFormat("cairowinGDIPrint");
91
map.resolution = e.Graphics.DpiX;
92
Console.WriteLine("map resolution = " + map.resolution.ToString() + "DPI defresolution = " + map.defresolution.ToString() + " DPI");
93
// Calculating the desired image size to cover the entire area;
94
map.width = Convert.ToInt32(e.PageBounds.Width * e.Graphics.DpiX / 100);
95
map.height = Convert.ToInt32(e.PageBounds.Height * e.Graphics.DpiY / 100);
97
Console.WriteLine("map size = " + map.width.ToString() + " * " + map.height.ToString() + " pixels");
99
IntPtr hdc = e.Graphics.GetHdc();
102
// Attach the device to the outputformat for drawing
103
of.attachDevice(hdc);
104
// Drawing directly to the GDI context
105
using (imageObj image = map.draw()) { };
109
of.attachDevice(IntPtr.Zero);
110
e.Graphics.ReleaseHdc(hdc);
113
e.HasMorePages = false;