0

I'm trying to figure out how to get an image added to the clipboard with an associated URI.

Adding a link to text is easy:

            string html = @"Version:0.9
                            StartHTML:<<<<<<<1
                            EndHTML:<<<<<<<2
                            StartFragment:<<<<<<<3
                            EndFragment:<<<<<<<4
                            SourceURL: <<<<<<<5
                            <html>
                            <body>
                            <!--StartFragment-->
                            <a href='aria: 73571 73570'>test 73571 73570</a>
                            <!--EndFragment-->
                            </body>
                            </html>";

            string link = html;
            Clipboard.SetText(link, TextDataFormat.Html);

but its not obvious what to do with a picture such as a bitmap.

Has anyone done this?

--- additional info ---

Just to clarify - the image I need to use is a bitmap generated by the program. I need to associate the image with a URI so that when pasted into something like Word, the user can click on the image to go to the link. Adding the bitmap to the clipboard on its own I can do, but its the URI part with it that I'm not sure of.

---- Another edit -----

I've tried going the embedded encoded image route by creating a string with the following in it:

Version:0.9
StartHTML:000089
EndHTML:9575818
StartFragment:000242
EndFragment:9575780
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML clipboard</title>
</head>
<body>
<!--StartFragment--><a href='aria: 73571 73570'><img src='data:image/png;base64,### Encoded Image removed for brevity ##'>
<!--EndFragment-->
</body>
</html>

if saved as an HTML file, this opens in a browser perfectly and the image lets me click on the link with no problem.

When I tried to use the clipboard:

        DataObject obj = new DataObject();
        obj.SetData(DataFormats.Html, new MemoryStream(encoding.GetBytes(htmlResult)));
        Clipboard.SetDataObject(obj, true);

htmlresult is the string containing the html fragment as shown before. When I try to paste into word, I can't get anything out of it.

I'm now running out of ideas..

Rob Marsh
  • 549
  • 5
  • 22
  • It's unclear what you are asking here. How can you possibly click on an image in the clipboard? Or into which application would you paste it that is able to follow the link? For some kind of "interactive" image you may perhaps create a HTML page. – Clemens Dec 04 '19 at 17:30
  • @Clemens It depends on how the application (in this case, Word) handles the pasted content. I've seen this kind of thing before, yes. See also, [this question](https://stackoverflow.com/q/57263058/395685) and my answer to it. – Nyerguds Jan 11 '20 at 10:09

1 Answers1

0

You can put multiple items on the clipboard simultaneously. Currently, you're only putting the html content on the clipboard, but not an actual image, and thus, applications pasting it will most likely not see it as image. You'll need to put both into the DataObject before putting it on the clipboard.

One thing I can see is an issue is the fact that the html fragment does not contain a url at all. It contains a raw data block as Base64. If you want it to point to a url, then you should give it a real url, rather than the data:image/png;base64, block. The html block you put on the clipboard is supposed to be additional metadata to go alongside an image, not the image itself.

Also, what is that aria: 73571 73570? That's not a url either.

Anyway, the proper way to put things onto the clipboard from stream is the following:

using (MemoryStream htmlStream = new MemoryStream(encoding.GetBytes(htmlResult)))
{
    DataObject obj = new DataObject();
    // The html content.
    obj.SetData(DataFormats.Html, htmlStream);
    // As standard bitmap
    obj.SetData(DataFormats.Bitmap, image);
    // The 'copy=true' argument means any streams that are used
    // can be safely disposed after the operation.
    Clipboard.SetDataObject(obj, true);
}

The mentioned image is the image. I'm not sure if this works the same way in wpf though; I've been doing this kind of stuff with the System.Drawing classes.

Do note that this kind of clipboard handling has no transparency support. Technically, the clipboard has no image transparency support, but certain programs can read other formats (like png) from the clipboard to get around that. See this answer for more information on that, though do note that that answer is not written for wpf.

Nyerguds
  • 5,360
  • 1
  • 31
  • 63