2
votes

I have a small program that is listening to clipboard(hook) for images. If there is an image stored or copied through ctrl+c and the like, my program will paste the image automatically to an open word doc.

Code:

if (Clipboard.ContainsImage())
{
  IDataObject obj = Clipboard.GetDataObject();
  if (obj.GetDataPresent(DataFormats.Bitmap))
   {
    Microsoft.Office.Interop.Word.Range range = thefile.Bookmarks.get_Item(@"\endofdoc").Range;
   range.Paste();
   newdoc.Selection.EndKey(WdUnits.wdStory);
   }
}

I just don't under stand why there are text from a certain application being copied to doc where in fact I'm only looking for bitmap data formats (based on my code). Is there a way to check if the data being copied is really an image or a text? I have no problem doing printscreen but every time I'll copy a text from a certain application (which I believe after doing ctrl+c is now stored in clipboard), the text is being considered an image?

1

1 Answers

0
votes

I think the reason is that Clipboard can contain various format data, such as text and bitmap, at the same time. So, when you run range.Paste() and Clipboard contains both text and bitmap data, text data will be copied.

To avoid this unexpected behavior, you need to get image data only from Clipboard by calling Clipboard.GetImage() and then paste it to your word doc.

Unfortunately I couldn't find any easy way to do that, but this answer might be helpful (i.e. write Clipboard image data to a file and then call Shapes.AddPicture to show the image in the word doc).