简体   繁体   English

如何整洁地将图像添加到剪贴板并粘贴?

[英]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.

相关问题 如何以编程方式添加面板并对其进行编辑? - How do I add panels programatically and edit them? 如何从硬盘获取图像,调整图像大小并将其添加到列表中 <image> 快速? - How can i get images from the hard disk resize the images and add them to a list<image> fast? C#如何将剪贴板中的格式化文本粘贴到RichTextBox - C# How can I paste formatted text from clipboard to the RichTextBox 如何使用 Selenium c# 将 url 从剪贴板粘贴到 Chrome 中的地址栏 - How can I paste url from clipboard to address bar in Chrome with Selenium c# 如何将自定义格式添加(附加)到剪贴板 - how can i add (append) custom format to clipboard 如何将OpenXML放到剪贴板上以便粘贴到Excel中? - How do I get OpenXML onto the clipboard so that it will paste into Excel? 如何以编程方式将条目添加到资源文件中? - How can I add entries into a Resource file programatically? 如何将图像添加到流中,然后逐步浏览它们? - How do I add images to a stream and then step through them? 如何在C#中将粘贴处理程序添加到IOleWindow - How can I add a paste handler to an IOleWindow in C# 如何在C#中干净地设计并行继承结构? - How can I cleanly design a parallel inheritance structure in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM