日期:2014-05-18  浏览次数:21085 次

二色TIF分割的问题。
现在公司的TIF分割文件,都是一些黑白二色的TIF。 

我把它们分割以后,分割的时候会自动转换成32位色,这样分割出来的小文件比原文件还大。我把它在从新转换成二色的文件的时候,会损失图像的清晰度。 

请问,如果实现二色TIF文件的分割和合成(就是把多个二色的tif文件合成为一个)?并且保持二色,清晰度不变? 

多谢指点!

------解决方案--------------------
http://www.codeproject.com/cs/media/SaveMultipageTiff.asp
------解决方案--------------------

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 */
} /* en