Can I copy and paste images from MS Office applications onto the Diagram?
Can I copy and paste images from MS Office applications onto the Diagram?
Essential Diagram does not have pre-built support for interfacing with clipboard copy/paste from the MS Office applications. The Diagram copy/paste implementation is confined to 'Node' type drawing objects that are native to Essential Diagram. To copy/paste an image, the Clipboard's data object should be an instance of the Diagram.ClipboardNodeCollection type populated with a Diagram.BitmapNode or a Diagram.MetafileNode that wraps the image. Since .NET uses its own format that is not compatible with the EnhancedMetafile format, you will have to use reflection to achieve this feature of copying and pasting images from MS Office applications like Excel, PowerPoint, Word, or Visio.
The sample provided in this Knowledge Base article demonstrates how you can add support to your Essential Diagram application to allow copy/paste from MS Office.
[C#]
using System.Runtime.InteropServices; using System.Reflection; public const uint CF_METAFILEPICT = 3; public const uint CF_ENHMETAFILE = 14; [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern bool CloseClipboard(); [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern IntPtr GetClipboardData(uint format); [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern bool IsClipboardFormatAvailable(uint format); //Handle EnhancedMetafile format from the clipboard and insert private void OnDiagramKeyUp(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { Metafile emf = null; if (OpenClipboard(IntPtr.Zero)) { if (IsClipboardFormatAvailable(CF_ENHMETAFILE)) { var ptr = GetClipboardData(CF_ENHMETAFILE); if (!ptr.Equals(IntPtr.Zero)) emf = new Metafile(ptr, true); } // You must close ir, or it will be locked CloseClipboard(); MetafileNode metanode = new MetafileNode(emf, new RectangleF(100, 100, 500, 500)); diagram1.Model.AppendChild(metanode); } } }
[VB]
'INSTANT VB NOTE: This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level: Public Const CF_METAFILEPICT As UInteger = 3 Public Const CF_ENHMETAFILE As UInteger = 14 <DllImport("user32.dll", CharSet := CharSet.Auto, ExactSpelling := True)> Public Shared Function OpenClipboard(ByVal hWndNewOwner As IntPtr) As Boolean End Function <DllImport("user32.dll", CharSet := CharSet.Auto, ExactSpelling := True)> Public Shared Function CloseClipboard() As Boolean End Function <DllImport("user32.dll", CharSet := CharSet.Auto, ExactSpelling := True)> Public Shared Function GetClipboardData(ByVal format As UInteger) As IntPtr End Function <DllImport("user32.dll", CharSet := CharSet.Auto, ExactSpelling := True)> Public Shared Function IsClipboardFormatAvailable(ByVal format As UInteger) As Boolean End Function Private Sub OnDiagramKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) If e.Control AndAlso e.KeyCode = Keys.V Then Dim emf As Metafile = Nothing If OpenClipboard(IntPtr.Zero) Then If IsClipboardFormatAvailable(CF_ENHMETAFILE) Then Dim ptr = GetClipboardData(CF_ENHMETAFILE) If Not ptr.Equals(IntPtr.Zero) Then emf = New Metafile(ptr, True) End If End If ' You must close ir, or it will be locked CloseClipboard() Dim metanode As New MetafileNode(emf, New RectangleF(100, 100, 500, 500)) diagram1.Model.AppendChild(metanode) End If End If End Sub
Sample: