private static Image GetImageFromParams(IntPtr handle, int pictype, IntPtr paletteHandle, int width, int height)
        {
            switch (pictype)
            {
            case -1:
                return(null);

            case 0:
                return(null);

            case 1:
                return(Image.FromHbitmap(handle, paletteHandle));

            case 2:
                Metafile metafile    = new Metafile(handle, new WmfPlaceableFileHeader(), false);
                object   objectValue = RuntimeHelpers.GetObjectValue(metafile.Clone());
                return((Image)objectValue);

            case 4:
            {
                Metafile metafile2    = new Metafile(handle, false);
                object   objectValue2 = RuntimeHelpers.GetObjectValue(metafile2.Clone());
                return((Image)objectValue2);
            }
            }
            throw new NotSupportedException();
        }
Пример #2
0
        public void Metafile_String()
        {
            string filename = Helpers.GetTestBitmapPath(WmfPlaceable);

            using (Metafile mf = new Metafile(filename))
                using (Metafile clone = (Metafile)mf.Clone())
                {
                }
        }
Пример #3
0
        /// ///////////////////////
        public Metafile GetImage(CContexteEvaluationExpression contexte)
        {
            Metafile imgToCopy = null;

            if (imgToCopy == null)
            {
                imgToCopy = ImageFile;
            }
            if (imgToCopy != null)
            {
                return((Metafile)imgToCopy.Clone());
            }
            return(null);
        }
Пример #4
0
        TryPasteEnhancedMetafile()
        {
            // This interop code was adapted from the following post:
            //
            // http://www.eggheadcafe.com/community/aspnet/12/10018506/
            // how-to-paste--metafile-fr.aspx
            //
            // NOTE: Do not try this non-interop technique:
            //
            //     Object oData = Clipboard.GetData(DataFormats.EnhancedMetafile);
            //
            //     if (oData != null)
            //     {
            //         System.Drawing.Imaging.Metafile oMetafile =
            //             (System.Drawing.Imaging.Metafile)oData;
            //     }
            //
            // When pasting from a chart image copied from Excel, the GetData()
            // call crashes Excel.  Although the Clipboard class reports that the
            // DataFormats.EnhancedMetafile format is available (along with
            // "Preferred DropEffect", "InShellDragLoop", and "MetaFilePict"), it
            // does not seem to be the format that the .NET Framework is expecting.
            // Using the interop technique below works properly, however.

            Boolean bReturn = false;

            if (OpenClipboard(this.Handle))
            {
                if (IsClipboardFormatAvailable(CF_ENHMETAFILE))
                {
                    IntPtr oPtr = GetClipboardData(CF_ENHMETAFILE);

                    if (!oPtr.Equals(new IntPtr(0)))
                    {
                        Metafile oMetafile = new Metafile(oPtr, true);

                        this.Image = (Metafile)oMetafile.Clone();
                        bReturn    = true;
                    }
                }

                CloseClipboard();
            }

            return(bReturn);
        }
        public static void Save(this Metafile metafile, Stream stream, bool forceWmfFormat)
        {
            if (metafile == null)
            {
                throw new ArgumentNullException(nameof(metafile), PublicResources.ArgumentNull);
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream), PublicResources.ArgumentNull);
            }
            if (!OSUtils.IsWindows)
            {
                throw new PlatformNotSupportedException(Res.RequiresWindows);
            }

            bool isWmf = metafile.RawFormat.Guid == ImageFormat.Wmf.Guid;

            if (isWmf || forceWmfFormat)
            {
                WriteWmfHeader(metafile, stream);
            }

            // making a clone, otherwise, it will not be usable after saving
            metafile = (Metafile)metafile.Clone();
            IntPtr handle = metafile.GetHenhmetafile();

            try
            {
                byte[] buffer = isWmf ? Gdi32.GetWmfContent(handle)
                        : (forceWmfFormat ? Gdi32.GetWmfContentFromEmf(handle) : Gdi32.GetEmfContent(handle));
                stream.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                if (isWmf)
                {
                    Gdi32.DeleteMetaFile(handle);
                }
                else
                {
                    Gdi32.DeleteEnhMetaFile(handle);
                }
                metafile.Dispose();
            }
        }
Пример #6
0
        private static Image?GetPictureFromParams(
            int handle,
            PICTYPE type,
            int paletteHandle,
            int width,
            int height)
        {
            switch (type)
            {
            case PICTYPE.ICON:
                return((Image)Icon.FromHandle((IntPtr)handle).Clone());

            case PICTYPE.METAFILE:
                WmfPlaceableFileHeader header = new WmfPlaceableFileHeader
                {
                    BboxRight  = (short)width,
                    BboxBottom = (short)height
                };

                using (var metafile = new Metafile((IntPtr)handle, header, deleteWmf: false))
                {
                    return((Image)metafile.Clone());
                }

            case PICTYPE.ENHMETAFILE:
                using (var metafile = new Metafile((IntPtr)handle, deleteEmf: false))
                {
                    return((Image)metafile.Clone());
                }

            case PICTYPE.BITMAP:
                return(Image.FromHbitmap((IntPtr)handle, (IntPtr)paletteHandle));

            case PICTYPE.NONE:
                // MSDN says this should not be a valid value, but comctl32 returns it...
                return(null);

            case PICTYPE.UNINITIALIZED:
                return(null);

            default:
                throw new ArgumentException("AXUnknownImage", nameof(type));
            }
        }
Пример #7
0
        public Metafile GetMetafile()
        {
            var f = FormatId.CF_METAFILEPICT.FormatEtc;

            DataObject.GetData(ref f, out var stg);
            try
            {
                if (stg.tymed != TYMED.TYMED_MFPICT)
                {
                    throw new ApplicationException();
                }
                using var hm = new Metafile(stg.unionmember, false);
                var ret = (Metafile)hm.Clone();
                return(ret);
            }
            finally
            {
                stg.Dispose();
            }
        }
Пример #8
0
 public void Metafile_String()
 {
     string   filename = WmfPlaceable;
     Metafile mf       = new Metafile(filename);
     Metafile clone    = (Metafile)mf.Clone();
 }
Пример #9
0
 /// <summary>
 /// Creates a view model from a <see cref="Metafile"/>.
 /// </summary>
 /// <param name="metafile">The metafile.</param>
 /// <param name="readOnly"><see langword="true"/>, to create a read-only instance; otherwise, <see langword="false"/>.</param>
 /// <returns>An <see cref="IViewModel{TModel}"/> instance that represents a view model for a <see cref="Metafile"/>.</returns>
 public static IViewModel <Metafile> FromMetafile(Metafile metafile, bool readOnly = false) => new ImageVisualizerViewModel(AllowedImageTypes.Metafile)
 {
     Image = (Metafile)metafile?.Clone(), ReadOnly = readOnly
 };