How to create a Print map method and a printpreview map

May 1, 2013 at 10:50 PM
Hi ,

I'm building a GIS application with dotspatial components and trying to create a print preview dialog and print dialog, so I figured to use the print dialog of windows forms but I don't know how to assign the map to the print. I tried this code but it does not work :

PrintPreviewDialog prv = new PrintPreviewDialog();
        prv.Document = this.map1;
I will be glad if someone can help me
May 2, 2013 at 7:22 AM
Hi,

This way works if you want to print the map control.
Private Sub PT_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PT.PrintPage

            Dim wid As Integer = Map1.Width
            Dim hgt As Integer = Map1.Height
            Dim img As New Bitmap(wid, hgt)

         
            Map1.DrawToBitmap(img, New System.Drawing.Rectangle(0, 0, wid, hgt))


            e.Graphics.DrawImage(img, e.MarginBounds.Left, e.MarginBounds.Top)


        End Sub
You can show the the map in the preview control
PrintPreviewControl1.Document = PT
May 2, 2013 at 10:07 AM
1/I tried your code , I used it like this :

"private void _print_Click(object sender, PrintPageEventArgs ppe)
    {
        int wid = map1.Width;
        int hei = map1.Height;
        Bitmap img = new Bitmap(wid, hei);
        map1.DrawToBitmap(img, new System.Drawing.Rectangle(0, 0, wid, hei));
        ppe.Graphics.DrawImage(img, ppe.MarginBounds.Left, ppe.MarginBounds.Top);

    }"
and I always an error : "No overload for 'print_Click' matches delegate 'System.EventHandler'

2/You told me if I want to use a printpreview , I use this code line : "PrintPreviewControl1.Document = PT"
what PT stands for ?!

Thanks
May 2, 2013 at 10:50 AM
You need to declare
Private WithEvents PT As New Printing.PrintDocument
PT stands for the PrintDocument

On the click event you say : PrintPreviewControl1.Document = PT

The PrintDocument is created this way
PT_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    int wid = Map1.Width;
    int hgt = Map1.Height;
    Bitmap img = new Bitmap(wid, hgt);


    Map1.DrawToBitmap(img, new System.Drawing.Rectangle(0, 0, wid, hgt));


    e.Graphics.DrawImage(img, e.MarginBounds.Left, e.MarginBounds.Top);


}
May 2, 2013 at 3:55 PM
I have tried it but with no result.
I will keep trying till it'll work.
Thank you.
Jun 27, 2013 at 1:51 PM
hi
i use this code but it doesn't work
     DotSpatial.Controls.LayoutForm frm = new DotSpatial.Controls.LayoutForm();
    string name = map1.Layers[0].LegendText;
    frm.MapControl = map1;
    frm.Show(); show a new form that is empty
Jul 1, 2013 at 8:18 AM
@anasr: I used it too, the same code, and it works (in vb.net, but I think it doesn't make any difference)

Oscar
Jul 1, 2013 at 6:18 PM
assuming the print function is trigger by a button click, here is c# code for printing the map

using System.Drawing.Printing;
      private void Print(object sender, PrintPageEventArgs ev)
        {

            int wid = map1.Width;
            int hgt = map1.Height;
            Bitmap img = new Bitmap(wid, hgt);


            map1.DrawToBitmap(img, new System.Drawing.Rectangle(0, 0, wid, hgt));
            ev.Graphics.DrawImage(img, ev.MarginBounds.Left, ev.MarginBounds.Top);
        }

    private void button7_Click(object sender, EventArgs e)
    {

        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler (this.Print);
        PrintDialog printDialog1 = new PrintDialog();
        printDialog1.Document = pd;
        DialogResult result = printDialog1.ShowDialog();

        // If the result is OK then print the document. 
        if (result == DialogResult.OK)
        {
            pd.Print();
        }
Jul 1, 2013 at 6:25 PM
actually it's run but show empty print page and dosent show any map that be shown in map1
tancks


Jul 3, 2013 at 3:56 PM
did you set

frm.MapControl = map1;

or whatever your map name is?

In my application it works, not perfectly but I can see into the print preview the maps I loaded in the main map

Oscar
Feb 10, 2014 at 7:40 AM
hi ytlee,
I followed your code in c# and it is printing the map but margins from left is more so it is cutting the right side of map. Kindly help me on the same.

Regards,
Dheeraj