将8Bit灰度数据转化为Bgr24的彩色图像
8bit灰度数据:byte[] GrayValue 长度:width * height
PixelFormat pixelFormat = PixelFormats.Bgr24;
byte[] colorvalues = GrayToColor(GrayValue);
ByteSize = Width * Height;
IntPtr dataPtr = IntPtr.Zero;
GCHandle _hObject = GCHandle.Alloc(colorvalues, GCHandleType.Pinned);//using System.Runtime.InteropServices;
dataPtr = _hObject.AddrOfPinnedObject();
BitmapSource bitmap = BitmapSource.Create(Width, Height, 0, 0, pixelFormat, null, dataPtr, ByteSize * 3, 3 *Width);
_hObject.Free();
public byte[] GrayToColor(byte [] grayvalue)
{
byte[] rgbValues = new byte[Height * Width * 3];
for (int i = 0; i < Height * Width; i++)
{
int j = i * 3;
if (grayvaluer[i] < 64)
{
rgbValues[j + 2] = 0;
rgbValues[j + 1] = (byte)(4 * grayvalue[i]);
rgbValues[j] = 255;
}
else if (grayvaluer[i] >= 64 && grayvalue[i] < 128)
{
rgbValues[j + 2] = 0;
rgbValues[j + 1] = 255;
rgbValues[j] = (byte)(2 * 255 - 4 * grayvalue[i]);
}
else if (grayvalue[i] >= 128 && grayvalue[i] < 192)
{
rgbValues[j + 2] = (byte)(4 * grayvalue[i] - 2 * 255);
rgbValues[j + 1] = 255;
rgbValues[j] = 0;
}
else
{
rgbValues[j + 2] = 255;
rgbValues [ j + 1] = (byte)(4 * 255 - 4 * grayvalue[i]);
rgbValues[j] = 0;
}
}
return rgbValues;
}