简体   繁体   中英

C# - Clipboard hooking check if the data being copied is really image or a text?

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?

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 (ie write Clipboard image data to a file and then call Shapes.AddPicture to show the image in the word doc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM