Articles in this section
Category / Section

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

5 mins read


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 the 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 it, 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

Conclusion

I hope you enjoyed learning about "Can I copy and paste images from MS Office applications onto the Diagram."

You can refer to the WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied