I am copying cells from an excel document to the clipboard so they can be inserted as an image elsewhere. The cells get copied to the clipboard fine, as I can paste the image manually after the code is run. However I cannot get ahold of the data. Here is my code:
tempWorkSheet.Range[tempWorkSheet.Cells[1, 1], tempWorkSheet.Cells[3, 3]].CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
// returns true
var test = Clipboard.GetDataObject().GetDataPresent(DataFormats.EnhancedMetafile);
// returns true
var test2 = Clipboard.ContainsData(DataFormats.EnhancedMetafile);
// returns null
var test3 = Clipboard.GetData(DataFormats.EnhancedMetafile);
// returns null
var test4 = Clipboard.GetDataObject().GetData(DataFormats.EnhancedMetafile);
The data is stored as an EnhancedMetaFile
and I can see the data in there but I cannot pull it out. I am at my wits end trying to figure this out. Does anybody see something I am missing?
I saw this question posted but it did not help me much. I'm hoping somebody can.
The problem is that you are trying to paste data in the same routine that copied it. You need to have separate handlers. Excel will use Delayed Rendering when you copy the image, and will have to have to process a rendering request in order to actually provide the image if/when requested. Meanwhile, other applications that have registered as Clipboard Viewers, are also processing the clipboard update. And here you are, insisting on immediate service for the image, without giving the system a chance to breath. You need to put your paste logic into a separate routine, so that it's not in the same call stack as the copy.
I use this code:
I found a solution. The
Clipboard.GetData(DataFormats.EnhancedMetafile)
call seems to be broken. But I managed to get it working using P/Invoke.It's not the prettiest code, but it works, so for posterity here it is in all it's glory:
Did you run the code from an STA thread? For example: