Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I copy and paste images, graphs, etc. from Microsoft 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#]

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

public const uint CF_METAFILEPICT = 3;
public const uint CF_ENHMETAFILE = 14;

private void PasteFromClipboard( PictureBox pictureBox )
{
  if ( OpenClipboard( 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
        pictureBox.Image = metafile;
      }
    }
    CloseClipboard();
  }
}