我一直在尋找SetPixel(的快速替代方法),我發現此鏈接:C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms AppARGB轉換到PARGB
所以我的問題是,我有一個形象,我想創建一個副本作爲DirectBitmap對象,但首先我需要ARGB轉換爲PARGB所以我用這個代碼:
public static Color PremultiplyAlpha(Color pixel)
{
return Color.FromArgb(
pixel.A,
PremultiplyAlpha_Component(pixel.R, pixel.A),
PremultiplyAlpha_Component(pixel.G, pixel.A),
PremultiplyAlpha_Component(pixel.B, pixel.A));
}
private static byte PremultiplyAlpha_Component(byte source, byte alpha)
{
return (byte)((float)source * (float)alpha/(float)byte.MaxValue + 0.5f);
}
,這裏是我的副本代碼:
DirectBitmap DBMP = new DirectBitmap(img.Width, img.Height);
MyImage myImg = new MyImage(img as Bitmap);
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
Color PARGB = NativeWin32.PremultiplyAlpha(Color.FromArgb(myImg.RGB[i, j].Alpha,
myImg.RGB[i, j].R, myImg.RGB[i, j].G, myImg.RGB[i, j].B));
byte[] bitMapData = new byte[4];
bitMapData[3] = (byte)PARGB.A;
bitMapData[2] = (byte)PARGB.R;
bitMapData[1] = (byte)PARGB.G;
bitMapData[0] = (byte)PARGB.B;
DBMP.Bits[(i * img.Height) + j] = BitConverter.ToInt32(bitMapData, 0);
}
}
MYIMAGE:含有位圖Ô類與一個結構RGB陣列一起存儲每個像素的顏色
但是,這段代碼給了我一個搞砸的圖像。我究竟做錯了什麼?
什麼是PARGB? (沒有雙關意圖,只是好奇) –
預乘Alpha,請在第一個鏈接 – Eslam