示例#1
0
        public static void MakeScreenshot(this AxMap map, Form parentForm)
        {
            var ext = map.Extents as MapWinGIS.Extents;

            var img = map.SnapShot3(ext.xMin, ext.xMax, ext.yMax, ext.yMin, map.Width);

            if (img != null)
            {
                using (var dlg = new SaveFileDialog())
                {
                    dlg.Filter = "*.jpg|*.jpg";
                    if (dlg.ShowDialog(parentForm) == DialogResult.OK)
                    {
                        if (!img.Save(dlg.FileName, false, ImageType.JPEG_FILE))
                        {
                            MessageHelper.Warn("Failed to save image: " + img.get_ErrorMsg(img.LastErrorCode));
                        }
                        else
                        {
                            MessageHelper.Warn("Image is saved: " + dlg.FileName);
                        }
                    }
                }
            }
        }
        private void PrintAsPDFAfterTilesAreLoaded()
        {
            AxMap.TilesLoaded -= AxMap_TilesLoaded;
            string      localdata      = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ResTBDesktop";
            string      localappdata   = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ResTBDesktop";
            PdfDocument mapTemplate    = PdfReader.Open(localappdata + "\\PrintTemplates\\A4_landscape.pdf", PdfDocumentOpenMode.Modify);
            PdfDocument outputDocument = new PdfDocument();

            outputDocument.PageLayout = mapTemplate.PageLayout;

            PdfPage page = outputDocument.AddPage();

            page.Orientation = mapTemplate.Pages[0].Orientation;
            page.Width       = mapTemplate.Pages[0].Width;
            page.Height      = mapTemplate.Pages[0].Height;

            int dx = (int)page.Width.Point, dy = (int)page.Height.Point;
            // calculate aspect
            var    diffX  = AxMap.Extents.xMax - AxMap.Extents.xMin;
            double aspect = ((double)dy / dx);
            int    diffY  = (int)(aspect * diffX);

            // start tile loading for cache
            Extents MapExtents = new Extents();

            MapExtents.SetBounds(AxMap.Extents.xMin, AxMap.Extents.yMin, AxMap.Extents.zMin, AxMap.Extents.xMax, AxMap.Extents.yMin + diffY, AxMap.Extents.zMax);


            // scale
            double meter            = AxMap.GeodesicDistance(AxMap.Extents.xMin, AxMap.Extents.yMin, AxMap.Extents.xMax, AxMap.Extents.yMin);
            double pageWidthInMeter = ((page.Width / 72) * 2.54) / 100;
            int    scale            = (int)(meter / pageWidthInMeter);

            int scaleRounded = scale % 100 >= 50 ? scale + 100 - scale % 100 : scale - scale % 100;

            if ((scale - scaleRounded < 10) && (scale - scaleRounded > -10))
            {
                scale = scaleRounded;
            }

            // Load the template stuff and change the acroforms...


            PdfAcroForm acroform = mapTemplate.AcroForm;

            if (acroform.Elements.ContainsKey("/NeedAppearances"))
            {
                acroform.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
            }
            else
            {
                acroform.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
            }

            var name = (PdfTextField)(acroform.Fields["ProjectTitle"]);

            changeFont(name, 12);
            name.Value = new PdfString(project.Name);

            var numberlabel = (PdfTextField)(acroform.Fields["ProjectNumberLabel"]);

            changeFont(numberlabel, 7);
            numberlabel.Value = new PdfString(Resources.Project_Number);
            var number = (PdfTextField)(acroform.Fields["ProjectNumber"]);

            changeFont(number, 7);
            number.Value = new PdfString(project.Number);

            var descriptionlabel = (PdfTextField)(acroform.Fields["DescriptionLabel"]);

            changeFont(descriptionlabel, 7);
            descriptionlabel.Value = new PdfString(Resources.Description);
            var description = (PdfTextField)(acroform.Fields["Description"]);

            changeFont(description, 7);
            description.Value = new PdfString(project.Description);

            var scalefield = (PdfTextField)(acroform.Fields["Scale"]);

            changeFont(scalefield, 10);
            scalefield.Value = new PdfString("1 : " + (int)scale);

            var legend = (PdfTextField)(acroform.Fields["LegendLabel"]);

            legend.Value = new PdfString(Resources.Legend);
            var copyright = (PdfTextField)(acroform.Fields["CopyrightLabel"]);

            copyright.Value = new PdfString("Impreso con " + Resources.App_Name);

            mapTemplate.Flatten();
            mapTemplate.Save(localdata + "\\printtemp.pdf");

            mapTemplate.Close();



            XGraphics gfx = XGraphics.FromPdfPage(page);

            var imageFromMap = AxMap.SnapShot3(AxMap.Extents.xMin, AxMap.Extents.xMax, AxMap.Extents.yMin + diffY, AxMap.Extents.yMin, (int)(dx * (96.0 / 72.0) * 2));

            imageFromMap.Save(localdata + "\\printTemp.tif", false, ImageType.TIFF_FILE);

            XImage image = XImage.FromFile(localdata + "\\printTemp.tif");
            // Left position in point
            double x = (dx - image.PixelWidth * 72 / image.HorizontalResolution) / 2;

            gfx.DrawImage(image, 0, 0, dx, dy);

            /*
             * XImage mapLayout = XImage.FromFile(localdata + "\\PrintTemplates\\A4_quer.png");
             *
             *
             * double width = mapLayout.PixelWidth * 72 / mapLayout.HorizontalResolution;
             * double height = mapLayout.PixelHeight * 72 / mapLayout.HorizontalResolution;
             *
             * gfx.DrawImage(mapLayout, (dx - width) / 2, (dy - height) / 2, width, height);
             */
            //outputDocument.AddPage(mapTemplateFilledOut.Pages[0]);


            XPdfForm form = XPdfForm.FromFile(localdata + "\\printtemp.pdf");

            gfx.DrawImage(form, 0, 0);

            outputDocument.Save(filename);
            image.Dispose();
            Process.Start(filename);
        }
示例#3
0
        /// <summary>
        /// Actual functionality to save a map image to a file
        /// </summary>
        /// <param name="handleMask"> shapefile mask layer</param>
        /// <returns></returns>
        private bool SaveMapHelper(int?handleMask)
        {
            //we now compute the width (w) that corresponds to a map whose width fits the required dpi
            var   ext     = _axMap.Extents;
            var   w       = ((double)_axMap.Width) * ((double)_dpi / 96);
            bool  proceed = true;
            Image img     = new Image();

            try
            {
                //create an image whose width (w) will result in a map whose width in pixels fits the the required dpi
                img = _axMap.SnapShot3(ext.xMin, ext.xMax, ext.yMax, ext.yMin, (int)w);
            }
            catch (System.Runtime.InteropServices.COMException comex)
            {
                Logger.Log(comex.Message, "SaveMapImage.cs", "SaveMapHelper");
                proceed = false;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message, "SaveMapImage.cs", "SaveMapHelper");
                proceed = false;
            }

            if (proceed)
            {
                //restore the map to its previous state by removing the mask and setting Reset to true
                if (handleMask != null)
                {
                    MapLayersHandler.RemoveLayer((int)handleMask);
                }
                Reset = true;
                AdjustFeatureSize();

                if (_saveToTempFile)
                {
                    _fileName = $@"{global.AppPath}\tempMap.jpg";
                    global.ListTemporaryFile(_fileName);
                    if (img.Save(_fileName))
                    {
                        img.Close();
                        return(true);
                    }
                    else
                    {
                        _fileName = $@"{global.AppPath}\tempMap1.jpg";
                        if (img.Save(_fileName))
                        {
                            img.Close();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    //specify filename of projection file for the image
                    var prjFileName = _fileName.Replace(Path.GetExtension(_fileName), ".prj");

                    //save the image to disk and create a worldfile. Image format is specified by USE_FILE_EXTENSION.
                    //also save the projection file
                    if (img.Save(_fileName, WriteWorldFile: true, FileType: ImageType.USE_FILE_EXTENSION) && _axMap.GeoProjection.WriteToFile(prjFileName))
                    {
                        //show the image file using the default image viewer
                        if (PreviewImage)
                        {
                            Process.Start(_fileName);
                        }
                        img.Close();
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }
        }