Repository: AvaloniaUI/Avalonia
File: src/Windows/Avalonia.Win32/OleDataObjectHelper.cs (C#)

Code before:

        if (format is DataFormat<byte[]> bytesFormat)
        {
            return dataTransfer.TryGetValue(bytesFormat) is { } bytes ?
                WriteBytesToHGlobal(ref hGlobal, bytes.AsSpan()) :
                DV_E_FORMATETC;
        }

        Logger.TryGet(LogEventLevel.Warning, LogArea.Win32Platform)
            ?.Log(null, "Unsupported data format {Format}", format);

        return DV_E_FORMATETC;
    }

    public unsafe static uint WriteDataToGdi(IDataTransfer dataTransfer, DataFormat format, ref IntPtr hGlobalBitmap)
    {
        if (ClipboardFormatRegistry.HBitmapDataFormat.Equals(format))
        {
            var bitmap = dataTransfer.TryGetValue(DataFormat.Bitmap);
            if (bitmap != null)
            {
                var pixelSize = bitmap.PixelSize;
                var stride = ((bitmap.Format?.BitsPerPixel ?? 0) / 8) * pixelSize.Width;
                var buffer = new byte[stride * pixelSize.Height];
                fixed (byte* bytes = buffer)
                {
                    bitmap.CopyPixels(new PixelRect(pixelSize), (IntPtr)bytes, buffer.Length, stride);

                    IntPtr hdc = IntPtr.Zero, compatDc = IntPtr.Zero, hbitmap = IntPtr.Zero;
                    try
                    {
                        hdc = GetDC(IntPtr.Zero);
                        if (hdc == IntPtr.Zero)
                            return DV_E_FORMATETC;

                        compatDc = CreateCompatibleDC(hdc);
                        if (compatDc == IntPtr.Zero)
                            return DV_E_FORMATETC;

                        hbitmap = CreateCompatibleBitmap(compatDc, pixelSize.Width, pixelSize.Height);
                        if (hbitmap == IntPtr.Zero)
                            return DV_E_FORMATETC;

                        SelectObject(compatDc, hbitmap);

                        var bitmapInfoHeader = new BITMAPINFOHEADER()
                        {
                            biWidth = pixelSize.Width,
                            biHeight = pixelSize.Height,
                            biPlanes = 1,
                            biBitCount = (ushort)(bitmap.Format?.BitsPerPixel ?? 0),

Review comment (suggestion):
we could probably do here without a copy, there is MemoryStream.GetBuffer(), but it might be larger than you need, so you need to use Length

Code after:
                        infoHeader.Init();

                        var imageData = new byte[infoHeader.bV5Size + infoHeader.bV5SizeImage];

                        fixed (byte* image = imageData)
                        {
                            Marshal.StructureToPtr(infoHeader, (IntPtr)image, false);
                            new Span<byte>(bytes, buffer.Length).CopyTo(new Span<byte>((image + infoHeader.bV5Size), buffer.Length));

                            return WriteBytesToHGlobal(ref hGlobal, imageData);
                        }
                    }
                }
            }
        }

        if (ClipboardFormatRegistry.PngSystemDataFormat.Equals(format)
            || ClipboardFormatRegistry.PngMimeDataFormat.Equals(format))
        {
            var bitmap = dataTransfer.TryGetValue(DataFormat.Bitmap);
            if (bitmap != null)
            {
                using var stream = new MemoryStream();
                bitmap.Save(stream);

                return WriteBytesToHGlobal(ref hGlobal, stream.ToArray().AsSpan());
            }
            return DV_E_FORMATETC;
        }

        if (format is DataFormat<string> stringFormat)
        {
            return dataTransfer.TryGetValue(stringFormat) is { } stringValue ?
                WriteStringToHGlobal(ref hGlobal, stringValue) :
                DV_E_FORMATETC;
        }

        if (format is DataFormat<byte[]> bytesFormat)
        {
            return dataTransfer.TryGetValue(bytesFormat) is { } bytes ?
                WriteBytesToHGlobal(ref hGlobal, bytes.AsSpan()) :
                DV_E_FORMATETC;
        }

        Logger.TryGet(LogEventLevel.Warning, LogArea.Win32Platform)
            ?.Log(null, "Unsupported data format {Format}", format);

        return DV_E_FORMATETC;
    }

    private static uint GetAlphaMask(Bitmap? bitmap)