代码之家  ›  专栏  ›  技术社区  ›  Jeff

Excel 2003 Windows窗体剪贴板数据对象

  •  1
  • Jeff  · 技术社区  · 14 年前

    图表图像可以很好地粘贴到mspaint或写字板中。

    我读过以下文章:

    Pasting Image from Excel 2003 Clipboard

    在我的特定场景中,PNG、JFIF或GIF格式作为格式出现在剪贴板数据对象上的唯一时间是我转到Excel剪贴板管理器(在Excel应用程序中)并在图表打开时再次复制它。

    也就是说,如果清除剪贴板,打开Excel 2003,创建一个图表,右键单击它,单击Copy,然后选中clipboard.GetDataObject().GetFormats(),我看到的就是:

    Clipboard.GetDataObject().GetFormats()
    {string[6]}
        [0]: "EnhancedMetafile"  // is null
        [1]: "Embed Source"  // is a MemoryStream of about 10KB which seems to be an OLE representation for the whole workbook
        [2]: "Object Descriptor" // a very short (10 bytes) MemoryStream
        [3]: "Link Source" // a very short (10 bytes) MemoryStream
        [4]: "Link Source Descriptor"  // a very short (10 bytes) MemoryStream
        [5]: "Link"  // null 
    

    Clipboard.GetDataObject().GetFormats()
    {string[10]}
        [0]: "Office Drawing Shape Format"
        [1]: "MetaFilePict"
        [2]: "EnhancedMetafile"
        [3]: "PNG+Office Art"  // can read with Image.FromStream
        [4]: "JFIF+Office Art"  // can read with Image.FromStream
        [5]: "GIF+Office Art"  // can read with Image.FromStream
        [6]: "PNG" // can read with Image.FromStream
        [7]: "JFIF" // can read with Image.FromStream
        [8]: "GIF" // can read with Image.FromStream
        [9]: "ActiveClipBoard"
    

    并且可以得到任何一种图像格式作为内存流,没有任何问题。

    我需要这个工作,而不必打开Excel剪贴板管理器。它在2007年和2010年运行良好(为了方便起见,它还包括常规位图格式,以便Clipboard.ContainsImage()返回true)。。。

    我想下一步可能是找到本地人 System.Runtime.InteropServices.ComTypes.IDataObject

    谢谢你的帮助。。。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jeff    14 年前

    哇…我希望这能帮助其他人。。。。

    在直接转到OLE32.dll之前,我有一个随机的想法尝试WPF剪贴板对象(System.Windows.clipboard)。。。。

    System.Windows.Clipboard.GetDataObject().GetFormats()
    {string[7]}
        [0]: "EnhancedMetafile"
        [1]: "System.Drawing.Imaging.Metafile"
        [2]: "Embed Source"
        [3]: "Object Descriptor"
        [4]: "Link Source"
        [5]: "Link Source Descriptor"
        [6]: "Link"
    

    果然:

    System.Windows.Clipboard.GetData(System.Windows.Clipboard.GetDataObject().GetFormats()[0])
        {System.Drawing.Imaging.Metafile}
    

    所以。。。

    ((Image)System.Windows.Clipboard.GetData(System.Windows.Clipboard.GetDataObject().GetFormats()[0])).Save("C:\\test1.jpg")
        base {System.Drawing.Image}: {System.Drawing.Imaging.Metafile}
    

    我怀疑直接使用OLE32.dll也会起作用,因为Windows窗体和WPF的剪贴板api都在那里。。。