我编写了下面的代码来操作图像的颜色。我想以某种方式撕开图像的每个像素。因此,对于每个像素,我想访问5位红色,6位绿色和5位蓝色(按照16位图像)。我如何改变我的代码来做到这一点?我想我必须以某种方式将我设置的字节值转换为位?字节到BMP中获取RGB的位
任何帮助将是伟大的。
private Bitmap InvertBitmap(Bitmap bmp)
{
unsafe
{
//create an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(bmp.Width, bmp.Height);
//lock the original bitmap in memory
System.Drawing.Imaging.BitmapData originalData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//lock the new bitmap in memory
System.Drawing.Imaging.BitmapData newData = newBitmap.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//set the number of bytes per pixel
int pixelSize = 3;
for (int y = 0; y < bmp.Height; y++)
{
//get the data from the original image
byte* originalImageRow = (byte*)originalData.Scan0 + (y * originalData.Stride);
//get the data from the new image
byte* newImageRow = (byte*)newData.Scan0 + (y * newData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
//set the new image's pixel to the inverted version
newImageRow[x * pixelSize] = (byte)(255 - originalImageRow[x * pixelSize + 0]); //B
newImageRow[x * pixelSize + 1] = (byte)(255 - originalImageRow[x * pixelSize + 1]); //G
newImageRow[x * pixelSize + 2] = (byte)(255 - originalImageRow[x * pixelSize + 2]); //R
}
}
//unlock the bitmaps
newBitmap.UnlockBits(newData);
bmp.UnlockBits(originalData);
return newBitmap;
}
}
565是16-bpp编码。 24-bpp图像有8位用于R,G和B,没有什么特别的需要。 – 2012-03-17 01:34:27
woops,你的权利,我想采取24位和操纵它,以便它是一个16位颜色的24位图像,对不起,将修复 – BigBug 2012-03-17 01:52:39
不知道这是否合理。我想你想和R和B与0xf8和G与0xfc设置低位为零。使用&运算符。很难看出差异,特别是在液晶显示器上。最快的方法就是将Graphics.DrawImage()图像转换为16bpp位图。 – 2012-03-17 01:55:19