BPS
BPS

Reputation: 677

C# Win8: copy .PNG to the Windows clipboard; paste with transparency preserved

I have been tasked with capturing an image, copy it to the clipboard, and paste it to the application below. I must be able to support pretty much any rich text field, and it must preserve transparency. My current solution first renders a white background. Here is my code:

The RenderTargetBitmap contains the image that I wish to copy as a .PNG

public static void CopyImageToClipboard(RenderTargetBitmap b)
{

    MemoryStream stream = new MemoryStream();
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(b));
    encoder.Save(stream);

    Bitmap bmp = new Bitmap(stream);
    Bitmap blank = new Bitmap(Convert.ToInt32(b.Width), Convert.ToInt32(b.Height));
    Graphics g = Graphics.FromImage(blank);
    g.Clear(System.Drawing.Color.White);
    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
    g.DrawImage(img, 0, 0, Convert.ToInt32(b.Width), Convert.ToInt32(b.Height));

    Bitmap tempImage = new Bitmap(blank);
    blank.Dispose();
    img.Dispose();

    bmp = new Bitmap(tempImage);
    tempImage.Dispose();

    System.Windows.Forms.Clipboard.SetImage(bmp);
    stream.Dispose();
 }

Upvotes: 3

Views: 1575

Answers (2)

Nyerguds
Nyerguds

Reputation: 5629

The Windows clipboard, by default, does not support transparency, but you can put content on the clipboard in many types together to make sure most applications find some type in it that they can use. Generally, if, in addition to the normal nontransparent clipboard Bitmap format, you put the image on the clipboard in both PNG and DIB formats, most applications will be able to use at least one of them to get the image in a format they support as being transparent.

These formats are put on the clipboard in a specific way, though. They need to have their data (for png, that's not the loaded image object but the actual png file's bytes) put in a MemoryStream that is then put on the clipboard.

PNG put on the clipboard like that will be accepted by a multitude of applications, including Gimp and the newer MS Office. And of course, if you implement reading for it too, you can use it in your own application. The (rather dirty) DIB format should take care of most other applications, if they indeed support transparent image pasting at all.

I detailed how to do both the copying and the retrieving in this answer:

https://stackoverflow.com/a/46424800/395685

Upvotes: 2

Adriano Repetti
Adriano Repetti

Reputation: 67090

Just pick a random color to use it as background, let's say

var background = Color.FromArgb(1, 255, 1, 255);

Erase background to it:

g.Clear(background); // instead of System.Drawing.Color.White

Then make that color transparent:

Bitmap tempImage = new Bitmap(blank);
tempImage.MakeTransparent(background);

Note that also default transparent color works pretty well, no need to pick a magic color (check if you need to Clear() background, it may be - I didn't check - default bitmap background):

g.Clear(Color.Transparent);
// ...
tempImage.MakeTransparent();

EDIT Some older RTF control versions won't handle transparency and there is nothing (AFAIK) you can do for it. A half decent workaround (if you can't detect control class of paste target and read its background color) is to use Color.FromArgb(254, 255, 255, 255). White where transparency isn't supported and...completely transparent (because of MakeTransparent()) where it is.

Upvotes: 3

Related Questions