2

I have made a program which monitors the clipboard and when a new image is copied, it gets saved to a file. Now when I copy an image from Paint.net which has transparent background, the background gets filled with white but when I copy the same image from Firefox it saves fine. Here is the test image i'm using: https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png and here is the code which I use to get the image from clipboard:

private Image GetImageFromClipboard()
    {
        if (Clipboard.GetDataObject() == null) return null;
        if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Dib))
        {
            var dib = ((System.IO.MemoryStream)Clipboard.GetData(DataFormats.Dib)).ToArray();
            var width = BitConverter.ToInt32(dib, 4);
            var height = BitConverter.ToInt32(dib, 8);
            var bpp = BitConverter.ToInt16(dib, 14);
            if (bpp == 32)
            {
                var gch = GCHandle.Alloc(dib, GCHandleType.Pinned);
                Bitmap bmp = null;
                try
                {
                    var ptr = new IntPtr((long)gch.AddrOfPinnedObject() + 40);
                    bmp = new Bitmap(width, height, width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, ptr);
                    return new Bitmap(bmp);
                }
                finally
                {
                    gch.Free();
                    if (bmp != null) bmp.Dispose();
                }
            }
        }
        return Clipboard.ContainsImage() ? Clipboard.GetImage() : null;
    }

Using the above code, I save the image:

Image img = GetImageFromClipboard();
img.RotateFlip(RotateFlipType.Rotate180FlipX);
img.Save("test.png");

I can also see the transparency in a picturebox when the image is copied from Firefox but not when copied from Paint.net.

user1365830
  • 171
  • 1
  • 11
  • How is the copy operation done each time? menu or shortcut? which menu commend, which shortcur? – TaW Nov 29 '15 at 15:25
  • In Paint.net I first press CTRL + A to select everything then CTRL + C to copy. In Firefox I just right-click on the image and press "copy". – user1365830 Nov 29 '15 at 16:04
  • OK. This seems to be a problem of the Clipboard. I can't copy tranparency from Paint.Net to Photoshop or back either. Of course Photoshop to Photoshop works; probably because they use an additional proprietray format.. I can't confirm that it works with firefox, at least not when doing a right-click and the copy graphics. – TaW Nov 29 '15 at 16:19
  • So there is nothing i can do to fix this? Could i not convert the format somehow? – user1365830 Nov 29 '15 at 17:36
  • You can try, but I'm a bit sceptical.. I didn't try, maybe if Paint.Net to Paint.Net works then the Clipboard must contain a format that includes Transparency. – TaW Nov 29 '15 at 17:46
  • Paint.net to Paint.net indeed works correctly and copying from Firefox to the program works but copying from Paint.net to the program wont work for some reason. – user1365830 Nov 29 '15 at 18:33
  • Ah. Ok, so the next step should be to look into the contents of the clipboard, read how many formats there are there; I would guess/hope there are more than just one.. – TaW Nov 29 '15 at 18:52
  • Or simply try to use GetImage ? – TaW Nov 29 '15 at 19:01
  • Darn. I hadn't tried it :-( - the next thing is to look into the DataPresent formats. [This](http://www.aspnet-answers.com/microsoft/NET-Drawing-GDI/32001808/bitmap--clipboard--transparency--blue-background.aspx) has an interesting discussion.. – TaW Nov 29 '15 at 19:15
  • I found [this](http://stackoverflow.com/a/998825/1365830) and tried the code; seems like paint.net uses a different format than firefox for copied images. Paint.net uses PaintDotNet.Rendering.MaskedSurface PNG format17 while Firefox uses some html format. My guess is that the Paint.net is a custom format that my program doesnt recognize properly. – user1365830 Nov 29 '15 at 19:23
  • Yes, that's what I think as well; isn't Paint.Net open source? So you could try the find out what they do.. – TaW Nov 29 '15 at 19:26
  • Apparently its no longer open source and all official source code were removed from their webpages so I guess i'm pretty much on my own here. – user1365830 Nov 29 '15 at 19:37
  • [This](https://code.google.com/p/openpdn/source/browse/#hg%2Fsrc%2FActions) may or may not help. See under [Actions.Copy](https://code.google.com/p/openpdn/source/browse/src/Actions/CopyToClipboardAction.cs) and [Actions.Paste](https://code.google.com/p/openpdn/source/browse/src/Actions/PasteAction.cs)! – TaW Nov 29 '15 at 19:52
  • It seems they use some sort of custom Bitmap class. I bet the MaskedSurface is responsible for transparency. Does 24bpp mean that there is no alpha channel and 32bpp has alpha channel? This is starting to seem like a bigger project than I had expected so I might have to look for some other way to do the task I had planned to use this program for.. – user1365830 Nov 29 '15 at 20:02
  • The `DataFormats` are just strings. A lot of apps nowadays copy it as type "PNG", with the actual data being a `MemoryStream` containing the bytes of the entire PNG image. I suggest starting with PNG, and falling back to DIB and eventually just standard clipboard Bitmap. – Nyerguds Oct 02 '17 at 14:25

0 Answers0