private unsafe BitmapSource CreateByOpacityColor(BitmapSource srcImage_, ulong colorKey)
{
BitmapSource srcImage = null;
if (srcImage_.Format == System.Windows.Media.PixelFormats.Bgr32 || srcImage_.Format == System.Windows.Media.PixelFormats.Bgra32)
{
srcImage = srcImage_;
}
else
{
// 先进行像素格式转换,再拷贝像素数据
srcImage = new FormatConvertedBitmap(srcImage_, System.Windows.Media.PixelFormats.Bgr32, null, 0);
}
int Width = srcImage.PixelWidth;
int Height = srcImage.PixelHeight;
Int32 stride = srcImage.Format.BitsPerPixel * Width / 8;
int bufSize = Height * stride;
PixelFormat format = System.Windows.Media.PixelFormats.Bgra32;
WriteableBitmap bmp_ = new WriteableBitmap(Width, Height, (double)96, (double)96, format, null);
bmp_.Lock();
byte* bDataScan0Addr = (byte*) bmp_.BackBuffer.ToPointer();
//ASSERT(bDataScan0Addr !=0);
Int32Rect cut = new Int32Rect(0, 0, Width, Height);
//调用CopyPixels
srcImage.CopyPixels(cut, (IntPtr)bDataScan0Addr, bufSize, stride);
//修改 alpha 位
UInt32* pixels = (UInt32*)bDataScan0Addr;
int pixelCount = bufSize / 4;
for (int i = 0; i < pixelCount; i++)
{
if ((pixels[i] & 0xffc0c0c0) == colorKey)
{
pixels[i] = 0x00000000;
}
}
bmp_.Unlock();
return bmp_;
}
C# BitmapSource 访问像素修改RGB
最新推荐文章于 2024-03-14 22:41:59 发布