【发布时间】: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