31.3 How can I copy and paste images/graphs etc from MS Office to a PictureBox?


Since .NET uses it's own format that is not compatible with the EnhancedMetafile format you will have to use reflection to achieve this. (From a posting in the microsoft.public.dotnet.framework.drawing newsgroup)


[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);


//Pasting into PictureBox
if (OpenClipboard(this.Handle))

{
     if (IsClipboardFormatAvailable(CF_ENHMETAFILE))
     {
          IntPtr ptr = GetClipboardData(CF_ENHMETAFILE);
          if (!ptr.Equals(new IntPtr(0)))
          {
               Metafile metafile = new Metafile(ptr,true);
               //Set the Image Property of PictureBox
               this.pictureBox1.Image = metafile;
          }
     }
     CloseClipboard();
}

© 2001-2010 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap