【问题标题】:C# copy image form PowerPoint to WordC# 将图像从 PowerPoint 复制到 Word
【发布时间】:2012-12-10 18:38:05
【问题描述】:

我需要一个应用程序来将文本和图像从 PowerPoint 复制到 Word。我使用以下库:Microsoft.Office.Interop.PowerPoint 和 Microsoft.Office.Interop.Word。

文本很容易传输,但是当我在 PowerPoint 中找到一个只包含图像的形状时,它会显示以下错误:“A generic error occurred GDI+”,在这部分代码:

foreach (PowerPoint.Shape shape in slide.Shapes)
{
   if (shape.HasTextFrame != MsoTriState.msoTrue){
      shape.Copy();
      Image img = (Image)Clipboard.GetData(DataFormats.Bitmap);
      string filepath = Environment.SpecialFolder.Desktop + "\\img.jpg";
      if (File.Exists(filepath))
      {
         File.Delete(filepath);
      }
      img.Save(filepath);
      doc.Shapes.AddPicture(filepath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
   }
}

在这种情况下,如何将包含图像的形状从 PowerPoint 复制到 Word? 欢迎任何帮助。我更喜欢一些代码示例。

谢谢。

【问题讨论】:

  • 是 shape.Copy() 还是 Clipboard.GetData(..) 失败了?
  • Clipboard.GetData(..) 失败
  • 该代码在我的 Win7 机器(.Net4 和 Office 2010)上运行良好。它在哪个操作系统下运行以及 .Net 的哪个版本?
  • 我有 .Net 3.5 和 Office 2007

标签: c# image ms-word powerpoint file-transfer


【解决方案1】:

如果你像这样重写你的代码,它会工作吗? GetImage 将进行自动转换以确保它是图像。如果您知道它是位图,您可以在代码中包含我的检查,以确保剪贴板实际包含图像。

shape.Copy();
bool imgOk = Clipboard.ContainsImage();
if (imgOk)
{
    Image img = Clipboard.GetImage();
    MessageBox.Show(imgOk.ToString());
    string filepath = @"c:\temp\img.jpg";
    if (File.Exists(filepath))
    {
        File.Delete(filepath);
    }
    img.Save(filepath);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    相关资源
    最近更新 更多