首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#24位位图转化成8位彩色图像解决思路

2014-01-03 
C#24位位图转化成8位彩色图像我有一幅1280*1024的24位彩色位图,想转化成8位彩色的位图,可是转换完成后发现

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 * stride + j] = index1;
                    }
                }               
                System.Runtime.InteropServices.Marshal.Copy(new8IndexBmpdataValues, 0, ptrNewBmpData, new8IndexBmpdataValues.Length);

                bitmap_new.UnlockBits(bitmapData_new);

                return bitmap_new;  
        }
[解决办法]
先转为gif,gif 就是8位
[解决办法]
图片属性中的位深度 我记得正在XP系统下如果是8位索引带ALPHA的话系统会认为是 32位的。

看图片是多少位的如果完全依赖这个或者依赖bitmap的pixelformat都有点靠不住的。 

还有你的图转换后的效果肯定和原图差的很多, 比PS的差多了。

热点排行