Exporting an Image from a WinForms application to PowerPoint with transparency
I've been having trouble pasting an image from my application into PowerPoint, while preserving transparency. I have an image stored as a System.Drawing.Graphics
type which I then convert to a System.Drawing.Bitmap
type and copy to the clipboard. During this process I also use Bitmap.MakeTransparent(Color.Black)
so that everything in the original document which was black will be transparent when the image is pasted.
if (GraphicsInterface.getGraphics() != null)
{
Image image = GraphicsInterface.getGraphics();
Bitmap bitmap = new Bitmap(image);
bitmap.MakeTransparent(Color.Black);
Clipboard.SetImage(bitmap);
}
However, when I try to paste the image into an application like PowerPoint, instead of being transparent, everything 开发者_如何学Cthat was black is now a very light gray.
Is my approach correct? Is there a way to reconcile the transparent values in .net and PowerPoint? Or will the transparency have to be done manually once the image is inserted to PowerPoint?
I was able to reproduce this issue by loading a known good file with transparency. I searched around a little bit and finally was able to come up with something that got the image onto the clipboard with transparency, which I then pasted into PowerPoint 2007 successfully.
You may still need to work some magic with the MakeTransparent()
operation, but this should get you started. Also, don't forget to dispose of the images properly. I omitted using
statements for clarity.
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);
精彩评论