~ubuntu-branches/ubuntu/saucy/mapserver/saucy-security

« back to all changes in this revision

Viewing changes to mapscript/csharp/examples/drawmapDirectPrint.cs

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-12-23 14:02:06 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111223140206-n3h9t2hsa8hyslmu
Tags: 6.0.1-2
Added missed stuff for libmapscript-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: drawmapDirectPrint.cs 10743 2010-11-19 17:05:38Z tbonfort $
 
3
 *
 
4
 * Project:  MapServer
 
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
 
8
 *
 
9
 ******************************************************************************
 
10
 * Copyright (c) 1996-2008 Regents of the University of Minnesota.
 
11
 *
 
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:
 
18
 *
 
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.
 
21
 *
 
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
 *****************************************************************************/
 
30
 
 
31
using System;
 
32
using System.Drawing;
 
33
using System.Drawing.Printing;
 
34
using OSGeo.MapServer;  
 
35
 
 
36
/// <summary>
 
37
/// A C# based mapscript example to draw the map directly onto a GDI printing device context.
 
38
/// </summary>
 
39
class DrawMap
 
40
{
 
41
  public static void usage() 
 
42
  { 
 
43
        Console.WriteLine("usage: DrawMapDirectPrint {mapfile} {printername}");
 
44
        System.Environment.Exit(-1);
 
45
  }
 
46
 
 
47
  static mapObj map;
 
48
                  
 
49
  public static void Main(string[] args)
 
50
  {
 
51
    Console.WriteLine("");
 
52
        if (args.Length < 2) usage();
 
53
    
 
54
        map = new mapObj(args[0]);
 
55
 
 
56
    Console.WriteLine("# Map layers " + map.numlayers + "; Map name = " + map.name);
 
57
    for (int i = 0; i < map.numlayers; i++) 
 
58
    {
 
59
        Console.WriteLine("Layer [" + i + "] name: " + map.getLayer(i).name);
 
60
    }
 
61
 
 
62
    try
 
63
    {
 
64
        PrintDocument doc = new PrintDocument();
 
65
 
 
66
        doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
 
67
 
 
68
        // Specify the printer to use.
 
69
        doc.PrinterSettings.PrinterName = args[1];
 
70
 
 
71
        doc.Print();
 
72
    } 
 
73
    catch (Exception ex) 
 
74
    {
 
75
                Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
 
76
                Console.WriteLine( 
 
77
                    "\nHelpLink ---\n{0}", ex.HelpLink );
 
78
                Console.WriteLine( "\nSource ---\n{0}", ex.Source );
 
79
                Console.WriteLine( 
 
80
                    "\nStackTrace ---\n{0}", ex.StackTrace );
 
81
                Console.WriteLine( 
 
82
                    "\nTargetSite ---\n{0}", ex.TargetSite );   }       
 
83
    }
 
84
 
 
85
    static void doc_PrintPage(object sender, PrintPageEventArgs e)
 
86
    {
 
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);
 
96
 
 
97
        Console.WriteLine("map size = " + map.width.ToString() + " * " + map.height.ToString() + " pixels");
 
98
 
 
99
        IntPtr hdc = e.Graphics.GetHdc();
 
100
        try
 
101
        {
 
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()) { };
 
106
        }
 
107
        finally
 
108
        {
 
109
            of.attachDevice(IntPtr.Zero);
 
110
            e.Graphics.ReleaseHdc(hdc);
 
111
        }
 
112
 
 
113
        e.HasMorePages = false;
 
114
    }
 
115
}
 
116