Knowledge Base



KB10932 - How to render map to a Metafile in DK for NET.

There is no direct possibility to render a map to a Metafile (WMF) in the DK.NET. There is, however, an easy way to subclass TGIS_RendererWinforms:

public class TGIS_RendererWMF : TGIS_RendererWinForms
{
    /// <summary>
    /// Print a current map view to a WMF
    /// </summary>
    /// <param name="_parent">Reference to a GIS viewer</param>
    /// <param name="_viewer">Reference to a GIS viewer. Same a _parent!</param>
    /// <param name="_stream">output stream</param>
    static public void PrintGisViewer(IGIS_ViewerParent _parent, IGIS_Viewer _viewer, System.IO.Stream _stream)
    {
        TatukGIS.NDK.WinForms.TGIS_RendererWinForms rnd;
        TGIS_RendererContext ctx;
        System.Drawing.Imaging.Metafile met;
        System.Drawing.Imaging.Metafile met1;
        System.Drawing.Imaging.Metafile met2;
        System.Drawing.Imaging.Metafile met3;
        System.Drawing.Imaging.Metafile met4;
        Graphics img;

        System.IO.MemoryStream mem1;
        System.IO.MemoryStream mem2;
        System.IO.MemoryStream mem3;
        System.IO.MemoryStream mem4;
        IntPtr dc;
        Graphics grp;


        grp = Graphics.FromHwnd((IntPtr)0);
        dc = grp.GetHdc();

        ctx = null;

        mem1 = null;
        mem2 = null;
        mem3 = null;
        mem4 = null;

        met1 = null;
        met2 = null;
        met3 = null;
        met4 = null;

        try
        {

            mem1 = new System.IO.MemoryStream();
            met1 = new System.Drawing.Imaging.Metafile(
                          mem1, dc,
                          new Rectangle(0, 0, _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight()),
                          System.Drawing.Imaging.MetafileFrameUnit.Pixel
                        );

            mem2 = new System.IO.MemoryStream();
            met2 = new System.Drawing.Imaging.Metafile(
                          mem2, dc,
                          new Rectangle(0, 0, _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight()),
                          System.Drawing.Imaging.MetafileFrameUnit.Pixel
                        );

            mem3 = new System.IO.MemoryStream();
            met3 = new System.Drawing.Imaging.Metafile(
                          mem3, dc,
                          new Rectangle(0, 0, _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight()),
                          System.Drawing.Imaging.MetafileFrameUnit.Pixel
                        );

            mem4 = new System.IO.MemoryStream();
            met4 = new System.Drawing.Imaging.Metafile(
                          mem4, dc,
                          new Rectangle(0, 0, _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight()),
                          System.Drawing.Imaging.MetafileFrameUnit.Pixel
                        );

            ctx = new TGIS_RendererContext();
            ctx.AssignBaseMap(
                  met1,
                  false
                );
            ctx.AssignSelection(
                  met2,
                  false
                );
            ctx.AssignCharts(
                  met3,
                  false
                );
            ctx.AssignLabels(
                  met4,
                  false
                );

            rnd = new TatukGIS.NDK.WinForms.TGIS_RendererWinForms();
            rnd.CreateContext(_parent, _viewer, ctx, new Point(0, 0), _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight(), 96, 100);
            _viewer.LabelsReg.Reset();
            rnd.BeforeDraw();
            _viewer.Draw(rnd, TatukGIS.NDK.TGIS_DrawMode.All);
            rnd.AfterDraw();
            rnd.ReleaseContext();
        }
        finally
        {

            ctx.Dispose();
            met1.Dispose();
            met2.Dispose();
            met3.Dispose();
            met4.Dispose();
        }


        met = null;
        img = null;

        try
        {
            met = new System.Drawing.Imaging.Metafile(
                         _stream, dc,
                         new Rectangle(0, 0, _parent.ControlCanvasWidth(), _parent.ControlCanvasHeight()),
                         System.Drawing.Imaging.MetafileFrameUnit.Pixel
                       );
            img = Graphics.FromImage(met);

            mem1.Position = 0;
            met1 = new System.Drawing.Imaging.Metafile(mem1);
            img.DrawImage(met1, 0, 0);
            met1.Dispose();
            mem1.Dispose();

            mem2.Position = 0;
            met2 = new System.Drawing.Imaging.Metafile(mem2);
            img.DrawImage(met2, 0, 0);
            met2.Dispose();
            mem2.Dispose();

            mem3.Position = 0;
            met3 = new System.Drawing.Imaging.Metafile(mem3);
            img.DrawImage(met3, 0, 0);
            met3.Dispose();
            mem3.Dispose();

            mem4.Position = 0;
            met4 = new System.Drawing.Imaging.Metafile(mem4);
            img.DrawImage(met4, 0, 0);
            met4.Dispose();
            mem4.Dispose();
        }
        finally
        {
            img.Dispose();
            met.Dispose();
        }

        grp.Dispose();
    }
}
Now creating a WMF file is possible with this code:
System.IO.FileStream strm = System.IO.File.Create(@"myfile.wmf");
TGIS_RendererWMF.PrintGisViewer(GIS, GIS, strm);
strm.Close();
Created: February 02, 2018, Modified: February 02, 2018