C#将图像形式PowerPoint复制到Word [英] C# copy image form PowerPoint to Word
问题描述
我需要一个应用程序将PowerPoint中的文本和图像复制到Word.我使用以下库:Microsoft.Office.Interop.PowerPoint和Microsoft.Office.Interop.Word.
I need a application to copy text and images form PowerPoint to Word. I use this libraries: Microsoft.Office.Interop.PowerPoint and Microsoft.Office.Interop.Word.
文本易于传递,但是当我在PowerPoint中发现仅包含图像的形状时,它显示此错误:"发生通用错误GDI + ",在此部分代码中:
The text s easy to transfer, but when I find in PowerPoint a shape containing only a image, it show this error: "A generic error occured GDI+", at this part of code:
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? 欢迎任何帮助.我更喜欢一些代码示例.
How can I copy a shape containing a image from PowerPoint to Word in this cirumstances? Any help is welcome. I prefer some code sample.
谢谢.
推荐答案
如果您以这种方式重写代码,它将起作用吗? GetImage将执行自动转换以确保它是图像.如果您知道这是位图,则可以在代码中包含我所进行的检查,以确保剪贴板实际上包含图像.
If you rewrite your code like this, will it work? GetImage will do an automatic conversion to make sure it's an image. If you know it's a bitmap you could include the check I have in the code to ensure the clipboard actually contains an image.
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);
}
这篇关于C#将图像形式PowerPoint复制到Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!