二色TIF分割的问题。 现在公司的TIF分割文件,都是一些黑白二色的TIF。 我把它们分割以后,分割的时候会自动转换成32位色,这样分割出来的小文件比原文件还大。我把它在从新转换成二色的文件的时候,会损失图像的清晰度。 请问,如果实现二色TIF文件的分割和合成(就是把多个二色的tif文件合成为一个)?并且保持二色,清晰度不变? 多谢指点! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 */ } /* 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 }编译的时候,需要把工程属性,生成项的允许不安全代码勾上 PixelFormat来控制色做图片识别二值化的时候用过。。 Encoder.Quality可以控制压缩质量,其它信息可以网上搜索一下。 如何计算FORM内大小 dataGridViewX1的用法提问 不知道在哪里问,就发这里吧 帮忙分析下代码,谢谢 有关Irame里的一些鼠标事件问题.. 线程的ThreadState竟然是96,请高手赐教! 怎样在C#工程中调用VC.net DLL HELP:我在线程中进行后台(WebClient网络下载)操作,可是前台的窗体还是阻塞(不能用鼠标移动等),而如果后台不是网络操作,而是其他的操作,UI C#制作一个边播边放的播放器~~高手进! 用C#搞小型信息管理系统开发最好看什么书? 如何把datagridview修改的数据保存到数据库中 获取当前mdi父窗体对象,使新打开的窗体成为子窗体
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
}编译的时候,需要把工程属性,生成项的允许不安全代码勾上
做图片识别二值化的时候用过。。