Ejemplo n.º 1
0
        private void exportAsButton__Click(object sender, EventArgs e)
        {
            if (metafileCanvas_.Metafile == null)
            {
                return;
            }

            using (var saveFileDialog = new SaveFileDialog())
            {
                var filters = new string[]
                {
                    "Bitmap Files (*.bmp;*.dib)|*.bmp;*.dib",
                    "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg",
                    "PNG Files (*.png)|*.png",
                    "Windows Meta Files (*.emf;*.wmf)|*.emf;*.wmf",
                    "All Files (*.*)|*.*",
                };

                saveFileDialog.FileName    = Path.GetFileNameWithoutExtension(filePath_);
                saveFileDialog.Filter      = string.Join("|", filters);
                saveFileDialog.FilterIndex = 3;

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    switch (Path.GetExtension(saveFileDialog.FileName).ToLower())
                    {
                    case ".emf":
#if false
                        // TODO:
                        // If orientation is changed, then the exported emf file can not be opened with
                        // this application anymore.
                        metafileCanvas_.Metafile.Save(saveFileDialog.FileName, ImageFormat.Emf);
#else
                        var metafile = metafileCanvas_.Metafile;
                        MetafileUtility.CopyMetafile(ref metafile, saveFileDialog.FileName);
                        metafileCanvas_.Metafile = metafile;
#endif
                        break;

                    case ".wmf":
                        metafile = metafileCanvas_.Metafile;
                        MetafileUtility.MetafileToWMF(ref metafile, saveFileDialog.FileName);
                        metafileCanvas_.Metafile = metafile;
                        break;

                    default:
                        using (var bitmap = MetafileUtility.MetafileToBitmap(metafileCanvas_.Metafile))
                        {
                            bitmap.Save(saveFileDialog.FileName);
                        }
                        break;
                    }
                }
            }
        }
        private void propertiesComboBox__SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedItem = propertiesComboBox_.SelectedItem;

            if (selectedItem == null)
            {
                return;
            }

            switch (selectedItem.ToString())
            {
            case "Metafile":
                propertyGrid_.SelectedObject = metafile_;
                break;

            case "MetafileHeader":
                propertyGrid_.SelectedObject = metafile_.GetMetafileHeader();
                break;

            case "ENHMETAHEADER":
                using (var metafile = MetafileUtility.CopyMetafile(ref metafile_))
                {
                    // Metafile.GetHenhmetafile is just a wrapper of GdipGetHemfFromMetafile:
                    // https://github.com/dotnet/runtime/blob/78c6505cffe2558b036fbe44cd27038affbb6cce/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.cs#L379-L383
                    // and according to:
                    // http://www.jose.it-berater.org/gdiplus/reference/flatapi/graphics/gdipgethemffrommetafile.htm
                    // > GdipGetHemfFromMetafile:
                    // > This method sets the Metafile object to an invalid state. The user is responsible for calling DeleteEnhMetafile,
                    // > to delete the Windows handle.
                    var enhMetafileHandle = metafile.GetHenhmetafile();
                    if (enhMetafileHandle != IntPtr.Zero)
                    {
                        var header = Gdi32.GetEnhMetaFileHeader(enhMetafileHandle);
                        propertyGrid_.SelectedObject = new EnhMetaHeader(header);

                        Gdi32.DeleteEnhMetaFile(enhMetafileHandle);
                    }
                }
                break;

            default:
                Debug.Assert(false);
                break;
            }
        }