从Excel 2003剪贴板粘贴图像 [英] Pasting Image from Excel 2003 Clipboard

查看:94
本文介绍了从Excel 2003剪贴板粘贴图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel文件中有图像,需要手动提取.我已经编写了一个程序,该程序对于使用Excel 2007的用户非常有效,但是不适用于Excel 2003.

I have images within an excel file, that I need to manually extract. I have written a program which functions very well for our users with Excel 2007, but will not work with Excel 2003.

过程: 用户打开Excel文件,将相关图像复制到剪贴板 用户打开C#应用程序,单击从剪贴板收集图像的按钮,然后从用户那里收集其他信息.

Process: User Opens Excel File, Copies Relevant Image to Clipboard User Opens C# Application, Clicks Button Which Gathers Image from Clipboard, and then gathers additional information from the user.

应用程序代码非常简单.单击按钮后,将调用以下代码:

The application code is pretty simple. On button click, the following code is called:

            if (Clipboard.GetImage() != null)
            {
                pictureBox1.Width = Clipboard.GetImage().Width;
                pictureBox1.Height = Clipboard.GetImage().Height;
                pictureBox1.Image = Clipboard.GetImage();

                //...more misc. code...
            }

这在Excel 2007上可以正常使用,但在Excel 2003上不能使用.

This works flawlessly with Excel 2007, but does not function with Excel 2003.

我尝试了以下调试代码,但都失败了:

I have attempted the following debug code, all which fails:

Clipboard.ContainsImage()>>返回false Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap)>> false

Clipboard.ContainsImage() >> returns false Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap) >> false

一个想法可能是Excel 2003"Office剪贴板"可能会干扰? MS PAINT粘贴图像没有问题.

One thought would be that the Excel 2003 "Office Clipboard" may be interfering? MS PAINT has no issue pasting the image however.

帮助?

推荐答案

我找到了解决方案.

显然,Excel 2007不会以相同的文件格式将图像复制到剪贴板.我遍历Clipboard.GetDataObject().GetFormats(),发现它包含以下内容:

Apparently Excel 2007 does not copy the image to the clipboard in the same file format. I iterated through Clipboard.GetDataObject().GetFormats() and found that it contained the following:

Office绘图形状格式 MetaFilePict 增强型图元文件 PNG +办公艺术 JFIF +办公艺术 GIF +办公室艺术 PNG 联合会 GIF ActiveClipboard

Office Drawing Shape Format MetaFilePict EnhancedMetafile PNG+Office Art JFIF+Office Art GIF+Office Art PNG JFIF GIF ActiveClipboard

要使其正常工作,我在代码中添加了第二个代码块,内容如下:

To get this to work, I've added a second codeblock to my code with the following:

        if (Clipboard.GetImage() != null) //Excel 2007
        {
            pictureBox1.Width = Clipboard.GetImage().Width;
            pictureBox1.Height = Clipboard.GetImage().Height;
            pictureBox1.Image = Clipboard.GetImage();
            //...
        }
        else if(Clipboard.GetDataObject().GetDataPresent("PNG")) //Excel 2003
        {
            Clipboard.GetDataObject().GetFormats()
            IDataObject data = Clipboard.GetDataObject();
            MemoryStream ms = (MemoryStream)data.GetData("PNG");

            pictureBox1.Width = Image.FromStream(ms).Width;
            pictureBox1.Height = Image.FromStream(ms).Height;
            pictureBox1.Image = Image.FromStream(ms);
            //...
        }

它有效.

这篇关于从Excel 2003剪贴板粘贴图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆