将位图复制到另一个位图后,围绕圆圈的黑色边框

时间:2014-08-15 17:00:05

标签: c# wpf bitmap

我从BitmapSource (RenderTargetBitmap)中提取了位图,其中包含蓝色圆圈。 RenderTargetBitmap是使用PixelFormats.Pbgra32创建的。

PixelFormats Pbgra32将每个颜色通道预先乘以alpha值。因此,当我尝试将位图转换为光标时,我的图像变得不那么透明。

我找到了问题here的解决方案,它将位图克隆到Format24bppRgb并手动设置R,B,G和alpha值。但是,解决方案完全正常,但对于克隆的位图,我看到了视觉上的黑色边框。

我可以摆脱克隆位图中的黑色边框吗? (我怀疑它是SafeCopy方法中的内容)

从链接中使用的方法是

private static void SafeCopy(BitmapData srcData, BitmapData dstData, byte alphaLevel)
{
  for (int y = 0; y < srcData.Height; y++)
    for (int x = 0; x < srcData.Width; x++)
    {
       byte b = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3);
       byte g = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3 + 1);
       byte r = Marshal.ReadByte(srcData.Scan0, y * srcData.Stride + x * 3 + 2);

       Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4, b);
       Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 1, g);
       Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 2, r);
       Marshal.WriteByte(dstData.Scan0, y * dstData.Stride + x * 4 + 3, alphaLevel);
    }
}

private static Cursor CreateCustomCursorInternal(Bitmap bitmap, double opacity)
{
    Bitmap cursorBitmap = null;
    IconInfo iconInfo = new IconInfo();
    Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

    try
    {
        byte alphaLevel = System.Convert.ToByte(byte.MaxValue * opacity);

        // Here, the pre-multiplied alpha channel is specified
        cursorBitmap = new Bitmap(bitmap.Width, bitmap.Height, 
                                   PixelFormat.Format32bppPArgb);

        // Assuming the source bitmap can be locked in a 24 bits per pixel format
        BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, 
                                                PixelFormat.Format24bppRgb);
        BitmapData cursorBitmapData = cursorBitmap.LockBits(rectangle, 
                               ImageLockMode.WriteOnly, cursorBitmap.PixelFormat);

        // Use SafeCopy() to set the bitmap contents
        SafeCopy(bitmapData, cursorBitmapData, alphaLevel);

        cursorBitmap.UnlockBits(cursorBitmapData);
        bitmap.UnlockBits(bitmapData);

        .......
}

原始位图

enter image description here

克隆位图

enter image description here

1 个答案:

答案 0 :(得分:2)

将WPF 32位PBGRA位图转换为WinForms PARGB位图并同时应用全局不透明度的最简单方法似乎是将所有A,R,G和B值与不透明度因子(浮点值之间)相乘0和1)类似于下面显示的方法。但是,我原本预计也需要交换字节,但显然它不是。

private static void CopyBufferWithOpacity(byte[] sourceBuffer,
    System.Drawing.Imaging.BitmapData targetBuffer, double opacity)
{
    for (int i = 0; i < sourceBuffer.Length; i++)
    {
        sourceBuffer[i] = (byte)Math.Round(opacity * sourceBuffer[i]);
    }

    Marshal.Copy(sourceBuffer, 0, targetBuffer.Scan0, sourceBuffer.Length);
}

给定32位PBGRA位图pbgraBitmap(例如RenderTargetBitmap),您将使用如下方法:

var width = pbgraBitmap.PixelWidth;
var height = pbgraBitmap.PixelHeight;
var stride = width * 4;
var buffer = new byte[stride * height];
pbgraBitmap.CopyPixels(buffer, stride, 0);

var targetFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
var bitmap = new System.Drawing.Bitmap(width, height, targetFormat);
var bitmapData = bitmap.LockBits(
    new System.Drawing.Rectangle(0, 0, width, height),
    System.Drawing.Imaging.ImageLockMode.WriteOnly,
    targetFormat);

CopyBufferWithOpacity(buffer, bitmapData, 0.6);

bitmap.UnlockBits(bitmapData);