示例#1
0
        private void FileExportImageMenuItem_Click(object sender, EventArgs e)
        {
            using (var dialog = new SaveFileDialog())
            {
                dialog.Filter           = "PNG Images|*.png|JPEG Images|*.jpg|BMP Images|*.bmp|Enhanced Metafiles (EMF)|*.emf|All Files|*.*||";
                dialog.Title            = "Export Image";
                dialog.InitialDirectory = PathHelper.SafeGetDirectoryName(Settings.LastExportImageFileName);
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    Settings.LastExportImageFileName = dialog.InitialDirectory;

                    var format = ImageFormat.Png;
                    var ext    = Path.GetExtension(dialog.FileName);
                    if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".jpg") == 0 ||
                        StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".jpeg") == 0)
                    {
                        format = ImageFormat.Jpeg;
                    }
                    else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".bmp") == 0)
                    {
                        format = ImageFormat.Bmp;
                    }
                    else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".emf") == 0)
                    {
                        format = ImageFormat.Emf;
                    }

                    var size = m_canvas.ComputeCanvasBounds(true).Size *m_canvas.ZoomFactor;
                    size.X = Numeric.Clamp(size.X, 16, 8192);
                    size.Y = Numeric.Clamp(size.Y, 16, 8192);

                    try
                    {
                        if (format == ImageFormat.Emf)
                        {
                            // export as a metafile
                            using (var nativeGraphics = Graphics.FromHwnd(m_canvas.Handle))
                            {
                                using (var stream = new MemoryStream())
                                {
                                    try
                                    {
                                        var dc = nativeGraphics.GetHdc();
                                        using (var metafile = new Metafile(stream, dc))
                                        {
                                            using (var imageGraphics = Graphics.FromImage(metafile))
                                            {
                                                using (var graphics = XGraphics.FromGraphics(imageGraphics, new XSize(size.X, size.Y)))
                                                {
                                                    m_canvas.Draw(graphics, true, size.X, size.Y);
                                                }
                                            }
                                            var handle = metafile.GetHenhmetafile();
                                            var copy   = CopyEnhMetaFile(handle, dialog.FileName);
                                            DeleteEnhMetaFile(copy);
                                        }
                                    }
                                    finally
                                    {
                                        nativeGraphics.ReleaseHdc();
                                    }
                                }
                            }
                        }
                        else
                        {
                            // export as an image
                            using (var bitmap = new Bitmap((int)Math.Ceiling(size.X), (int)Math.Ceiling(size.Y)))
                            {
                                using (var imageGraphics = Graphics.FromImage(bitmap))
                                {
                                    using (var graphics = XGraphics.FromGraphics(imageGraphics, new XSize(size.X, size.Y)))
                                    {
                                        m_canvas.Draw(graphics, true, size.X, size.Y);
                                    }
                                }
                                bitmap.Save(dialog.FileName, format);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(Program.MainForm, string.Format("There was a problem exporting the map:\n\n{0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }