日期:2014-05-17  浏览次数:21166 次

C#24位位图转化成8位彩色图像
我有一幅1280*1024的24位彩色位图,想转化成8位彩色的位图,可是转换完成后发现位深度为32.转化时往新图片里写的是byte,怎么能是32位深度呢?求指教。
 public Bitmap ConvertToBitmap8 (Bitmap bmp) 
        { 
                int Width = bmp.Width;
                int Height = bmp.Height;
                Rectangle rect = new Rectangle(0, 0, Width, Height);
                Bitmap bitmap_new = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
                byte[] byteOldbitmapColorValues = GetBmp24BitColorValue(bmp);// 将传入的Bitmap统一为24位  
                BitmapData bitmapData_new = bitmap_new.LockBits(rect, ImageLockMode.WriteOnly, bitmap_new.PixelFormat);
                IntPtr ptrNewBmpData = bitmapData_new.Scan0;
                int stride = Math.Abs(bitmapData_new.Stride);//Stride 每行byte数的个数  
                byte[] new8IndexBmpdataValues = new byte[stride * Height];
                int oldBitmapColorValuesIndex = 0;               
                for (int i = 0; i != Height; i++)
                {
                    for (int j = 0; j != stride; j++)
                    {
                        byte index1 = CompareColor(byteOldbitmapColorValues[oldBitmapColorValuesIndex + 2],
                                  byteOldbitmapColorValues[oldBitmapColorValuesIndex + 1],
                                  byteOldbitmapColorValues[oldBitmapColorValuesIndex], bitmap_new.Palette.Entries);
                        oldBitmapColorValuesIndex += 3;  //返回调色板索引值
                        new8IndexBmpdataValues[i&n