I'm trying to use the System.Windows.Clipboard
class to obtain an image from the clipboard:
var bitmapSource = System.Windows.Clipboard.GetImage();
When the image is copied via the PrintScreen key, it works fine. However, when the image is copied from a medical application, I get the following exception:
System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
at System.Runtime.InteropServices.ComTypes.IDataObject.GetData(FORMATETC& format, STGMEDIUM& medium)
at System.Windows.DataObject.OleConverter.GetDataInner(FORMATETC& formatetc, STGMEDIUM& medium)
at System.Windows.DataObject.OleConverter.GetDataFromOleOther(String format, DVASPECT aspect, Int32 index)
at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert, DVASPECT aspect, Int32 index)
at System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert)
The image pastes fine on Paint and Word, so the image is being copied properly to the clipboard. It's not a huge image, so I'm definitely not running out of memory. Any ideas?
Calling Clipboard.GetDataObject().GetFormats()
returns the following:
{string[11]}
[0]: "Rich Text Format"
[1]: "MetaFilePict"
[2]: "PNG+Office Art"
[3]: "Office Drawing Shape Format"
[4]: "DeviceIndependentBitmap"
[5]: "Bitmap"
[6]: "System.Drawing.Bitmap"
[7]: "System.Windows.Media.Imaging.BitmapSource"
[8]: "Format17"
[9]: "EnhancedMetafile"
[10]: "System.Drawing.Imaging.Metafile"
I tried Clipboard.GetData(format)
for each of the formats above, and the only ones that returned a non-null object were "PNG+Office Art", "Office Drawing Shape Format", "Format17", and "EnhancedMetafile".