如何在大概保持原来图像的颜色的情况下,将32位bmp文件改为8位即256色图像。

解决方案 »

  1.   

    Bitmap bmp = Bitmap.FromFile("文件");
    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Gif);
    Image gif = Image.FromStream(ms);
    gif.Save("文件2.bmp", ImageFormat.Bmp);//这里保存的就是8位色深的位图了。
      

  2.   

    Bitmap构造函数像素格式PixelFormat所以你可以用后面两个构造函数,比如
    Bitmap bmp = new Bitmap(_with, _height, PixelFormat.Format8bppIndexed)
      

  3.   

    我前段时间做过代码发你看看!
      public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)
            {
                //// 申请目标位图的变量,并将其内存区域锁定
                Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
                     System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);            //// 获取图像参数
                int stride = bmpData.Stride;  // 扫描线的宽度
                int offset = stride - width;  // 显示宽度与扫描线宽度的间隙
                IntPtr iptr = bmpData.Scan0;  // 获取bmpData的内存起始位置
                int scanBytes = stride * height;   // 用stride宽度,表示这是内存区域的大小            //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
                int posScan = 0, posReal = 0;   // 分别设置两个位置指针,指向源数组和目标数组
                byte[] pixelValues = new byte[scanBytes];  //为目标数组分配内存            for (int x = 0; x < height; x++)
                {
                    //// 下面的循环节是模拟行扫描
                    for (int y = 0; y < width; y++)
                    {
                        pixelValues[posScan++] = rawValues[posReal++];
                    }
                    posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
                }
     Bitmap bmp1 = ToGrayBitmap(this.newFile.FileData, 512, 4096);                string filename = Path.GetFileNameWithoutExtension(this.newFile.FileName);
                    bmp1.Save(Path.Combine(ImgPath,filename) + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
      

  4.   

    C#转换图形的色深
    http://hi.baidu.com/touchthememory/blog/item/ecaeed3f8048c4ca7c1e716e.html