views:

43

answers:

1

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 that 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?

+2  A: 

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);
kbrimington
Note that the "PNG" format is only supported by *Microsoft Office*.
AMissico
Thanks, @AMissico. Do you mean by this that other office-like products, such as OOo.org, will not accept an image added to the clipboard in this way? (I'll not be able to confirm this at the office.)
kbrimington
@kbrmington: (See http://stackoverflow.com/questions/2799442/need-help-setting-an-image-with-transparent-background-to-clipboard for reference.) From my experimentation, I believe the "PNG" clipboard format is a custom format implemented by *Microsoft Office*. I thought this was a solution to my problem but found it only works with *Microsoft Office* applications. I was able to test with Office 2003 and Office 2007. I also tested with all graphics applications on my computer and none of them understood the format. I used the "Clipboard Viewer" to monitor the clipboard while testing.
AMissico
@AMissico: Thanks. I'm going to put some thought into this. What are you pasting into?
kbrimington
@kbrimington: I normally use *Paint Shop Pro 9*. Yet, I was trying to get the image to paste into anything. I think a good application to test with would be *Paint.NET* since it is free.
AMissico
Thanks, this works well. To make the image transparent I just created a bitmap, made the necessary color transparent, and cast it back to an image.
Patrick
@Patrick: I'm glad it helped. Do keep an eye on @AMissico's question (referenced in the comments) in case a more-portable solution surfaces.
kbrimington