将多个(例如图像和文本)内容复制到剪贴板以粘贴到 MS office C# Winform

Copy multiple (e.g. image and text) things to the Clipboard for pasting into MS office C# Winform

我想将多个内容复制到剪贴板:

Clipboard.SetImage(Image);
Clipboard.SetText(ImageCaption);  

但是我注意到所有 Clipboard.Set* 函数首先清除剪贴板。所以在上面的例子中你只能得到文本。有没有 "add" 等价物?

至少我希望能够将位图和一些文本添加到剪贴板上,以便可以将它们一起粘贴到 Microsoft Office 等应用程序中。

我厚着脸皮试了一下:

    [Serializable]
    public class ImageAndText
    {
        public readonly string text;
        public readonly Bitmap image;

        public ImageAndText(string text, Bitmap image)
        {
            this.text = text;
            this.image = image;
        }
    }
    private void CopyToClipBoard()
    {

        Clipboard.SetData(DataFormats.Rtf , new ImageAndText(NewLayer.RecursionLog, rootLayer.GetBitmapTrueRes()));
    }

但刚刚得到了图像和文本的 "to string",这是有道理的。必须有一种数据格式可以用来告诉 MS 它正在获取图像和文本吗?这是秘密吗?

目前我正在使用 RTF 进行这项工作,但对于我想要实现的目标来说它似乎有点过分了...

我抓住了这个 nice little RTF library 代码项目,感谢 Dima 与世界分享这个:)

然后我修改了它,添加了一个额外的功能来写入剪贴板(我确信额外的功能可以添加得更好但它有效)('cough' hacky'cough')

将我的图像和文本复制到剪贴板的代码变为:

private void CopyDetailToClipBoard(string caption, Image image)
{

    //using rtf data format, put a bunch of data onto the clipboard
    RtfDocument rtf = new RtfDocument();
    RtfParagraph rtfTextBlock = new RtfParagraph();             
    const int captionFontSize = 22;


   //add main image to rtf text
    RtfImage image = new RtfImage(image, RtfImageFormat.Png);
    rtfTextBlock.AppendParagraph(image);

    //add caption
    RtfFormattedText Caption = new RtfFormattedText(caption,
        RtfCharacterFormatting.Underline | RtfCharacterFormatting.Bold,
        1,
        captionFontSize);      

    rtfTextBlock.AppendParagraph(Caption);

    rtf.Contents.Add(rtfTextBlock);

    //write the rtf to the clipboard
    RtfWriter rtfWriter = new RtfWriter();
    rtfWriter.WriteToClipBoard(rtf);
}

我添加到 RTF 库的额外函数是 "WriteToClipbard",我添加到 RtfWriter.cs,它只是写函数的 C&P,但不是将字符串写入文件,而是写入文件剪贴板 (如果我将 RtfDocument 修改为仅具有可在任何一种情况下使用的 "tostring" 会更好)

//Where rtfWriter.WriteToClipBoard is a modified version of the "Write" function:
public void WriteToClipBoard(RtfDocument document)
{
    _encoding = Encoding.GetEncoding((int)document.CodePage);

    sb = new StringBuilder();

    Reflect(document);

    Clipboard.SetData(DataFormats.Rtf, sb.ToString());
}

我仍然希望有一个 "lighter" 解决方案然后这个,但这适用于从我的应用程序复制到 Word/any rtf 应用程序并且它有一个很好的好处,我可以进行精美的格式化等.

p.s。

警告一句,如果您只是为了好玩而向图像(或 vice-versa)添加文本,不要,因为警告仅将位图添加到剪贴板会被更多应用程序(例如绘画)识别,同样,将 RTF 写入记事本也不是很干净:p。

所以您的 实际上限制了潜在的 cross-over(请参阅下面的更新) 您的应用程序数据通过 RTF路线。对我来说,文本实际上不仅仅是一个 "caption" 它是一整套数据,解释了图像是如何创建的(因此为什么花哨的格式实际上非常有用,但为了简洁起见我已经简化了所有这些)。我实际上我发现自己将其添加为 "cntrl+shift+c" 选项并保留 "ctnrl+c" 以将一个好的旧位图添加到剪贴板。

--更新---

事实证明,您可以将 RTF 文件 复制并粘贴到 Word 中。因此,您可以跳过额外的写入剪贴板部分并创建文件,然后将文件添加到剪贴板。这实际上允许您拥有多个图像+文本项目,缺点是它作为嵌入式文档粘贴到 word 中,但这实际上更适合我想要的。

另外,您 可以解决应用程序限制问题 ,因为您可以将多个类型并行添加到剪贴板。所以我目前将它作为 RTF 文件和位图添加到剪贴板。做“setimage" does not overwrite the "setfile

通过这种方式,当我粘贴到 word 中时,我得到了带有图像和位图的 RTF。

当我粘贴到绘画中时,我只粘贴图像 :)(实际上,当我添加到位图剪贴板时,我使用 GDI 来写标题)

它在各种程序中的工作方式实际上非常酷。有趣的是,不同的办公程序有不同的默认值 "choices"。 Word 似乎会选择 RTF,而 Powerpoint 会选择位图。