1

I'm trying to copy an image to the windows clipboard but windows keeps removing my alpha. This is may latest attempt where i manually create the buffer and copy the BGRA values to the buffer, however still when i read the values later alpha is set to 255. If i copy my image from Paint.NET I get an InteropBitmap with the exact same values but alpha is set correct, so it should be possible. If i save my image to a PNG-file instead of the clipboard alpha is also correct.

var buffer = new byte[width * height * 4];
for (var x = 0; x < width; x++)
{
    for (var y = 0; y < height; y++)
    {
        var pixel = surface[x, y];
        var i = (y * width + x) * 4;
        buffer[i + 0] = pixel.B;
        buffer[i + 1] = pixel.G;
        buffer[i + 2] = pixel.R;
        buffer[i + 3] = pixel.A;
    }
}
Clipboard.Clear();
Clipboard.SetImage(InteropBitmap.Create(
    bounds.Width, bounds.Height,
    96, 96,
    PixelFormats.Bgra32,
    null,
    buffer,
    width * 4));
Andreas
  • 6,958
  • 10
  • 38
  • 52
  • "If i save my image to a PNG-file instead of the clipboard alpha is also correct" are you using `InteropBitmap.Create` in that implementation? I suppose we can assume the values in `surface[x, y]` are correct then? in particular the `pixel.A` value. – Brett Caswell Oct 27 '19 at 21:25
  • 1
    possible [duplicate](https://stackoverflow.com/questions/44177115/copying-from-and-to-clipboard-loses-image-transparency) of this question actually. (searched `Clipboard.SetImage Alpha`) – Brett Caswell Oct 27 '19 at 21:27

0 Answers0