[英]How can I cleanly programatically add images to the clipboard and paste them?
i have a global registered hotkey which works fine, inside this hotkey is the following code: 我有一个全局注册的热键,它可以正常工作,在此热键内是以下代码:
{
Clipboard.SetDataObject(Properties.Resources.cookie);
SendKeys.SendWait("^v");
}
It adds the cookie image to my clipboard and pastes it in which ever window is activated. 它将cookie图像添加到我的剪贴板中,并将其粘贴到激活了窗口的位置。 However it only works properly the first time and pastes the image. 但是,它只能在第一次正常运行并粘贴图像。
The second time i get: A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll 我第二次得到:System.Windows.Forms.dll中发生了'System.Runtime.InteropServices.COMException'类型的首次机会异常
and this pops up: 然后弹出:
However this only pops up if the program has this option, otherwise nothing happens (but i still get the exception error). 但是,如果程序具有此选项,则只会弹出此窗口,否则不会发生任何事情(但我仍然会收到异常错误)。
But if i manually press ctrl+V... it works every time :S 但是如果我手动按ctrl + V ...它每次都可以工作:S
The Clipboard
class has a different method that might work better for you: Clipboard
类有一个不同的方法,可能对您更好:
Clipboard.SetImage(...);
When setting DataObject
only, you can also specify the type of the object. 仅设置DataObject
,还可以指定对象的类型。 This would also be a viable option, that probably works the same exact way, but using the appropriate method seems.. well.. more appropriate. 这也将是一个可行的选择,它的工作方式可能完全相同,但是使用适当的方法似乎更合适。
Another important note: 另一个重要说明:
Clipboard
method's only work on "Single Apartment" threads, so if you're not performing the action on the UI thread, you should make sure to set the thread's state to STA
. Clipboard
方法仅适用于“ Single Apartment”线程,因此,如果您不对UI线程执行操作,则应确保将线程的状态设置为STA
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.