Skip to main content
 首页 » 编程设计

c#之以透明方式将图像从 WinForms 应用程序导出到 PowerPoint

2024年02月24日26EasonJim

我一直无法将应用程序中的图像粘贴到 PowerPoint 中,同时又要保持透明度。我将图像存储为 System.Drawing.Graphics 类型,然后将其转换为 System.Drawing.Bitmap 类型并复制到剪贴板。在此过程中,我还使用了 Bitmap.MakeTransparent(Color.Black),以便在粘贴图像时原始文档中的所有黑色内容都将透明。

if (GraphicsInterface.getGraphics() != null) 
{ 
    Image image = GraphicsInterface.getGraphics(); 
    Bitmap bitmap = new Bitmap(image); 
    bitmap.MakeTransparent(Color.Black); 
    Clipboard.SetImage(bitmap); 
} 

但是,当我尝试将图像粘贴到 PowerPoint 等应用程序中时,所有原本黑色的东西现在都变成了非常浅的灰色,而不是透明的。

我的做法是否正确?有没有办法协调 .net 和 PowerPoint 中的透明值?还是在将图像插入到 PowerPoint 后必须手动完成透明度?

请您参考如下方法:

我能够通过透明加载已知良好的文件来重现此问题。我四处搜索了一下,终于想出了一些办法,可以将透明图像放到剪贴板上,然后我成功地将其粘贴到 PowerPoint 2007 中。

您可能仍需要对 MakeTransparent() 操作施展魔法,但这应该能让您入门。另外,不要忘记妥善处理图像。为了清楚起见,我省略了 using 语句。

Image image = Image.FromFile(@".\Star.png"); 
MemoryStream stream = new MemoryStream(); 
image.Save(stream, ImageFormat.Png); 
DataObject data = new DataObject("PNG", stream); 
Clipboard.SetDataObject(data, true);