【问题标题】:Setting Alpha values for a Format32bppArgb为 Format32bppArgb 设置 Alpha 值
【发布时间】:2011-05-11 19:04:23
【问题描述】:

我一直在尝试为格式为 Format32bppArgb 的位图手动设置 alpha 值。在这个代码示例中,我将它们全部设置为 0.5f,但是,它们将来会是不同的值,而不是全部 0.5f/128(因为这是我的测试用例,只是为了让它工作)。如何快速正确设置位图的 alpha 值?我可以使用 SetPixel(),但是与仅锁定/解锁位图相比,SetPixel() 对于大图像来说速度非常慢。

        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        for (int counter = 0; counter < rgbValues.Length; counter += 4)
        {
            rgbValues[counter] = 255;
            rgbValues[counter + 1] = 255;
            rgbValues[counter + 2] = 255;
            rgbValues[counter + 3] = 128;
        }

        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        // Unlock the bits.
        bmp.UnlockBits(bmpData);

【问题讨论】:

  • 你能告诉我们为什么你不能以同样的方式使用吗?使用你的 alpha 值 * 255?

标签: c# algorithm image image-processing bitmap


【解决方案1】:

如果您希望在整个位图上具有相同的 alpha 值,那么最好的方法是使用 ColorMatrix。看看微软的这个例子:

http://msdn.microsoft.com/en-us/library/w177ax15(v=vs.71).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2020-06-27
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多