C # Win8: Copy .PNG to Windows Clipboard; transparent paste

I was tasked with capturing an image, copying it to the clipboard, and pasting it into the app below. I should be able to support just about any textbox and it should be transparent. My current solution displays a white background first. Here is my code:

The RenderTargetBitmap contains the image that I want to copy as .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();
 }

      

+3


source to share


2 answers


Just pick a random color to use as a background, say

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

      

Erase it:

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

      

Then make this color transparent:



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

      

Note that also the default transparent color works very well, no need to choose a magic color (check if you need a background Clear()

, it could be - I haven't tested - the default bitmap background):

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

      

EDIT . Some older versions of RTF control won't handle transparency, and there is nothing (AFAIK) you can do to do this. Half a decent workaround (if you can't define the paste target control class and read its background color) is to use Color.FromArgb(254, 255, 255, 255)

. White where transparency is not supported and ... completely transparent (due to MakeTransparent()

) where it is.

+3


source


The Windows Clipboard does not support transparency by default, but you can put many types of clipboard content together to make sure most applications find some type in it that they can use. Generally, if, in addition to the normal opaque clipboard bitmap format, you put an 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 that they support being transparent.

These formats are placed on the clipboard in a specific way. They must have their data (for png, that not the loaded image object, but the actual bytes of the png file) placed in MemoryStream

, which is then placed on the clipboard.

PNG placed on the clipboard will be accepted by a variety of applications, including Gimp and the new MS Office. And of course, if you are also reading, you can use it in your application. The (rather messy) DIB format should take care of most other applications if they really support transparent image nesting at all.



I have detailed how to do both copy and receive in this answer:

fooobar.com/questions/636771 / ...

0


source







All Articles