SharpMap v0.9 Print

Topics: General Topics
Dec 16, 2008 at 4:38 PM

I haven't been able to find a reference to v0.9 having a print  function.

Could someone point me to a method, if one exists?

Bob

Coordinator
Dec 17, 2008 at 9:52 AM
Hi Bob, 0.9 doesn't have a print function (nor does 2 for that matter) ... hth jd
Dec 17, 2008 at 9:57 AM
Edited Dec 17, 2008 at 10:01 AM
Hi Bob... unforunately you'll have to create your own. something like below

        /// <summary>
        /// A printable map
        /// </summary>
        /// <param name="factor">Factor by which to multiply map size to get a decent resulotion.</param>
        /// <returns></returns>
        private Image printImage(int factor)
        {
            Size s = new Size(mapImage.Image.Width * factor, mapImage.Image.Height * factor);
            SharpMap.Map map = new SharpMap.Map();
            map = Code.mapHelper.PrintMap(s, 5, mapImage.Map.Zoom, mapImage.Map.Center);
            map.Center = mapImage.Map.Center;
            map.Zoom = mapImage.Map.Zoom;

            Bitmap b = new Bitmap(map.GetMap());
            return b;

        }

        // Page setup
        private void pageSetupFileItem_Click(object sender, System.EventArgs e)
        {
            try
            {
                pageSetupDialog1.Document = printDocument1;
                pageSetupDialog1.ShowDialog();
            }
            catch (InvalidPrinterException)
            {
                MessageBox.Show(this, "Failed accessing printer device", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Print preview
        private void printPreviewFileItem_Click(object sender, System.EventArgs e)
        {

            try
            {
                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.ShowDialog();
            }
            catch (InvalidPrinterException)
            {
                MessageBox.Show(this, "Failed accessing printer device", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        // Print document page
        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap image = null;
            image = (Bitmap)printImage(5);
            System.Diagnostics.Debug.WriteLine("X: " + e.MarginBounds.Left + ", Y = " + e.MarginBounds.Top + ", width = " + e.MarginBounds.Width + ", height = " + e.MarginBounds.Height);
            System.Diagnostics.Debug.WriteLine("X: " + e.PageBounds.Left + ", Y = " + e.PageBounds.Top + ", width = " + e.PageBounds.Width + ", height = " + e.PageBounds.Height);

            int width = image.Width;
            int height = image.Height;
            if ((width > e.MarginBounds.Width) || (height > e.MarginBounds.Height))
            {
                float f = Math.Min((float)e.MarginBounds.Width / width, (float)e.MarginBounds.Height / height);
                width = (int)(f * width);
                height = (int)(f * height);
            }

            e.Graphics.DrawImage(image, e.MarginBounds.Left, e.MarginBounds.Top, width, height);
        }

Cheers.. and pls... don't ask about print to scale. That's not funny at all :)

Forgot to mention that i have a special map set up in the MapHelper Class. It takes an int as input. its a factor that you want to use to multiply fontsizes and line width etc. Very dirty hack but a feasable way of getting a descent print resolution.

Dec 23, 2008 at 6:23 PM

Hi Peter

In this part of the code you suggested "map = Code.mapHelper.PrintMap(s, 5, _sharpMap.Zoom, _sharpMap.Center);"

What does 'Code' refer to? It doesn't seem to be part of mapHelper.

Bob

Dec 28, 2008 at 10:00 AM
Hi Bob

Code is not a namespace in Sharpmap but in my application. MapHelper is a class in that namespace. Printmap is a method in that class that returns SharpMap.Map.

Example:

/// <summary>
        /// A map formatted for printing and saving as image.
        /// </summary>
        /// <param name="s">The Size. Should be a factor of the map shown on screen</param>
        /// <param name="factor">The factor. is used for multiplying line thickness, symbol size, font size etc</param>
        /// <returns></returns>
        public static SharpMap.Map PrintMap(Size s, int factor, double zoom, SharpMap.Geometries.Point center)
        {
            //Initialize a new map
            SharpMap.Map map = new SharpMap.Map(s);
          
            //Getting the default path for auxiliary shapefiles
            string initpath = Properties.Settings.Default.GeoDataPath;

            //Districts
            VectorLayer layDistricts = new VectorLayer("Districts");
            layDistricts.DataSource =
  new SharpMap.Data.Providers.PostGIS(Configuration.AppConfiguration.ConnectionString, "vv_get_adm_area_district", "polygon", "gid");
            layDistricts.Style.Fill = new SolidBrush(Color.AntiqueWhite);
            layDistricts.Style.EnableOutline = true;
            layDistricts.Style.Outline = new Pen(Color.LightBlue, 1.5f * factor);
            layDistricts.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

      map.Layers.Add(layDistricts);
            map.BackColor = Color.White;
            map.Center = center;
            map.Zoom = zoom;          
            return map;
        }
Hope that helps

Peter