4

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".

11
  • Sounds like a job for serialization (though not sure it's a dupe in this instance).
    – Chris W.
    Dec 19, 2018 at 17:33
  • @ChrisW. The thing is that my application doesn't copy anything to the clipboard; it only pastes, so the data should already be serialized.
    – redcurry
    Dec 19, 2018 at 17:36
  • Note that GDI+ gives an OutOfMemoryException for a huge load of things going wrong that are completely unrelated to actually being low on memory.
    – Nyerguds
    Dec 19, 2018 at 17:38
  • @Nyerguds Yes, I don't believe it's memory-related.
    – redcurry
    Dec 19, 2018 at 17:41
  • Have you actually added a reference to System.Drawing.Image? Because, you use var as type shortcut and called the variable bitmapSource... but Clipboard.GetImage() does not return a System.Windows.Media.Imaging.BitmapSource object.
    – Nyerguds
    Dec 19, 2018 at 17:43

1 Answer 1

3

I believe your answer is here. In short:

the conclusion is that if you are working with the Clipboard in WPF and you are getting System.OutOfMemoryExceptions that don’t seem to make any sense, then you’ve probably forgotten to add the SerializableAttribute to whatever class you placed on the Clipboard.

So is this medical application your application? Because it would seem the problem is with how the image is put in the clipboard, rather than how the image is retrieved.

Update: Since it is not your application, then you will likely have to put up with their mistake (or the mistakes in Clipboard.GetData()). The source code of Clipboard.GetImage() is this:

public static Image GetImage() {
    var dataObject = Clipboard.GetDataObject();
    if (dataObject != null) {
        return dataObject.GetData(DataFormats.Bitmap, true) as Image;
    }

    return null;
}

Notice that your stack trace says that the exception happened in GetData(). Looking at the source code, that means that the call to GetDataObject() worked, which means you could (theoretically) use GetDataObject() yourself and convert the IDataObject from that into something you can use.

It might take some exploring to figure out what's going on. You might be able to use IDataObject.GetFormats() to inspect what it is, then use IDataObject.GetData() to get the data in that format.

Update 2: The solution from here points us in the right direction, but needs some modification:

var data = Clipboard.GetDataObject();
var ms = (MemoryStream) data.GetData("PNG+Office Art");

var image = Image.FromStream(ms)
17
  • It's not my medical application, so I have no control over it. But I believe that the image is copied to the clipboard correctly because I can paste it to Paint and Word.
    – redcurry
    Dec 19, 2018 at 17:39
  • Well, if it's an image, 'adding attributes' is irrelevant since Image is a framework library.
    – Nyerguds
    Dec 19, 2018 at 17:44
  • 1
    @redcurry Note that there are many ways of copying an image to the clipboard... and MS Office prefers a PNG MemoryStream object because classic windows image clipboard has no transparency support. You may need to dig deeper into what exactly is on that clipboard.
    – Nyerguds
    Dec 19, 2018 at 17:52
  • 2
    See here: stackoverflow.com/a/1406304/1202807 (use a MemoryStream) Seems @Nyerguds was right about it being a weird PNG MemoryStream. Dec 19, 2018 at 18:22
  • 1
    Huh, seems I linked to the wrong post... the one in which I detailed multiple clipboard formats is actually mentioned in a comment on that answer. By the way, Bitmap is the standard implementation of the abstract Image class, so all that means is that the MemoryStream contained the bytes for a valid image file. If you have some WPF-specific way to open these bytes as image it might be simpler to do that directly.
    – Nyerguds
    Dec 20, 2018 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.