We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Can I copy and paste images from MS Office applications onto the Diagram?

Platform: WinForms |
Control: Diagram |
Published Date: April 30, 2011 |
Last Revised Date: May 31, 2019

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:

Sample

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
JunJoy
Feb 15, 2018

Hello

I use the code in vs2012 but it didn't work. When I copy symbol from visio2016, and here "IntPtr ptr = GetClipboardData(CF_ENHMETAFILE);" always return 0. Another article https://www.syncfusion.com/forums/71540/how-to-import-visio-stencils-to-symbol-palettes says visio version affect the convert code, does it affect copy too? I'm dying for help, thank you. 

Reply
JunJoy
Feb 15, 2018

Hello

I try some new code from https://stackoverflow.com/questions/18602171/copy-enhanced-metafile-from-clipboard-and-save-it-as-an-image/29923975#29923975 and it seems work well. Maybe here is the reson if (OpenClipboard(IntPtr.Zero))

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon