现在公司的TIF分割文件,都是一些黑白二色的TIF。 我把它们分割以后,分割的时候会自动转换成32位色,这样分割出来的小文件比原文件还大。我把它在从新转换成二色的文件的时候,会损失图像的清晰度。 请问,如果实现二色TIF文件的分割和合成(就是把多个二色的tif文件合成为一个)?并且保持二色,清晰度不变? 多谢指点! 

解决方案 »

  1.   

    http://www.codeproject.com/cs/media/SaveMultipageTiff.asp
      

  2.   


    Bitmap如果读入的是高位色的图片后再用ColorDepth降低无效,后来我查了些资料,可以通过转成Gif再保存,这样就可以转为低位的了代码如下:        private void button10_Click(object sender, EventArgs e)
            {
                Bitmap bmp = new Bitmap("c:\\a.bmp");
                SaveGIFWithNewColorTable(bmp, "c:\\b.bmp", 1, false);
            }        protected void SaveGIFWithNewColorTable(Image image, string filename, uint nColors, bool fTransparent)
            {
                if (nColors > 256)
                    nColors = 256;
                if (nColors < 2)
                    nColors = 2;            int Width = image.Width;
                int Height = image.Height;            Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);            ColorPalette pal = GetColorPalette(nColors);            for (uint i = 0; i < nColors; i++)
                {
                    uint Alpha = 0xFF;                      // Colors are opaque.
                    uint Intensity = i * 0xFF / (nColors - 1);    // Even distribution.                 if (i == 0 && fTransparent) // Make this color index...
                        Alpha = 0;          // Transparent                pal.Entries[i] = Color.FromArgb((int)Alpha,
                                                    (int)Intensity,
                                                    (int)Intensity,
                                                    (int)Intensity);
                }            bitmap.Palette = pal;
                Bitmap BmpCopy = new Bitmap(Width,
                                        Height,
                                        PixelFormat.Format32bppArgb);
                {
                    Graphics g = Graphics.FromImage(BmpCopy);                g.PageUnit = GraphicsUnit.Pixel;                g.DrawImage(image, 0, 0, Width, Height);                g.Dispose();
                }            BitmapData bitmapData;
                Rectangle rect = new Rectangle(0, 0, Width, Height);            bitmapData = bitmap.LockBits(
                    rect,
                    ImageLockMode.WriteOnly,
                    PixelFormat.Format8bppIndexed);
                IntPtr pixels = bitmapData.Scan0;            unsafe
                {
                    byte* pBits;
                    if (bitmapData.Stride > 0)
                        pBits = (byte*)pixels.ToPointer();
                    else
                        pBits = (byte*)pixels.ToPointer() + bitmapData.Stride * (Height - 1);
                    uint stride = (uint)Math.Abs(bitmapData.Stride);                for (uint row = 0; row < Height; ++row)
                    {
                        for (uint col = 0; col < Width; ++col)
                        {
                            Color pixel;    // The source pixel.                        byte* p8bppPixel = pBits + row * stride + col;                        pixel = BmpCopy.GetPixel((int)col, (int)row);                        double luminance = (pixel.R * 0.299) +
                                (pixel.G * 0.587) +
                                (pixel.B * 0.114);
                            *p8bppPixel = (byte)(luminance * (nColors - 1) / 255 + 0.5);                    } /* end loop for col */
                    } /* end loop for row */
                } /* end unsafe */            bitmap.UnlockBits(bitmapData);            bitmap.Save(filename, ImageFormat.Gif);            BmpCopy.Dispose();
                bitmap.Dispose();        }        protected ColorPalette GetColorPalette(uint nColors)
            {
                // Assume monochrome image.
                PixelFormat bitscolordepth = PixelFormat.Format1bppIndexed;
                ColorPalette palette;    // The Palette we are stealing
                Bitmap bitmap;     // The source of the stolen palette            // Determine number of colors.
                if (nColors > 2)
                    bitscolordepth = PixelFormat.Format4bppIndexed;
                if (nColors > 16)
                    bitscolordepth = PixelFormat.Format8bppIndexed;            // Make a new Bitmap object to get its Palette.
                bitmap = new Bitmap(1, 1, bitscolordepth);            palette = bitmap.Palette;   // Grab the palette            bitmap.Dispose();           // cleanup the source Bitmap            return palette;             // Send the palette back
            }编译的时候,需要把工程属性,生成项的允许不安全代码勾上
      

  3.   

    PixelFormat来控制色
    做图片识别二值化的时候用过。。
      

  4.   

    Encoder.Quality可以控制压缩质量,其它信息可以网上搜索一下。