0

Reference post: CLIPBRD_E_CANT_OPEN error when setting the Clipboard from .NET

I already tried the methods in the thread above. But they didn't work. I asked a question there, and was directed to open a new question myself, hence here.

Basically, I turned a usercontrol into a RenderTargetBitmap. I am trying to pass the new RenderTargetBitmap object to the clipboard, so that other applications(suchs as paint/Word/Powerpoint) can use the image(similar to copy image on Google image).

I am running the application in Virtual Box and it seems that the Virtual Box is constantly accessing the clipboard. So, I used the code below but it took literally 10 seconds to finish copying to the clipboard. It also freezes the GUI at the same time. I tried async method, so that the GUI is not frozen. But it still takes around 10 seconds to finish copying to the clipboard. Then, I tried making a new thread. I set that thread to STA. But for some reasons, when I pasted the image to Paint, it said the information on clipboard cannot be inserted.

RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);

for (int i = 0; i < 10; i++)
{
    try{
        Clipboard.SetImage(bmp);
        return;
    }
    catch(Exception e) { }
    System.Threading.Thread.Sleep(10);

}

Any help is highly appreciated!

H.T. Kong
  • 151
  • 5
  • Have you seen this? https://stackoverflow.com/q/2522380/1136211 – Clemens Aug 27 '19 at 22:16
  • Thanks for your response!!! Sorry for the confusion. I already did those steps. You can assume the bmp is of class RenderTargetBitmap. The issue is that when I call this line "Clipboard.SetImage(bmp);" I experienced an issue similar to this post https://stackoverflow.com/questions/68666/clipbrd-e-cant-open-error-when-setting-the-clipboard-from-net – H.T. Kong Aug 27 '19 at 22:52
  • `SetImage()` usually executes very fast. _"...other process is using it"_. You should check which process is blocking the clipboard. Maybe a restart can fix it. – BionicCode Aug 27 '19 at 23:27
  • Thanks for the advice! It seems that b/c I am running the application in Oracle Virtual Box and I enabled shared clipboard, virtual box is constantly locking the clipboard... Is there a way for the application to lock/close the all access to the clipboard before calling the setImage()? I tried some methods in this post but I couldn't get it work...https://stackoverflow.com/questions/6583642/determine-which-process-is-locking-the-clipboard – H.T. Kong Aug 28 '19 at 02:24
  • I suspect that `RenderTargetBitmap` might be doing the actual rendering lazily when the Clipboard is trying to read the content of the `ImageSource` (in this case `RenderTargetBitmap` object) you passed. So that would take longer to render the actual visual and might also be the reason for the exception when executed on another thread and that is triggering the render. Try to copy the `RenderTargetBitmap` object to a normal in-memory image thus forcing the render ahead of copying to clipboard and see if this reduces the negative effects you're getting. – Mohammad Aug 28 '19 at 13:04
  • Thanks for the response! I will try that out and let you know! – H.T. Kong Aug 28 '19 at 13:12

0 Answers0