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

Platform: WinForms| Category: 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();
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.