为什么有的save是24位而有的是8位

woyaoshi 2010-07-04 07:48:04

private void button1_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{

//curBitmap为我定义的 一个载入图片 为 form的私有成员变量
//新的我要的8位图像容器
Bitmap bit = new Bitmap(curBitmap.Width,curBitmap.Height,PixelFormat.Format8bppIndexed);
//可读写的方式锁定全部原图像
BitmapData data = curBitmap.LockBits(new Rectangle(0, 0, curBitmap.Width,curBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//得到首地址
IntPtr ptr1 = data.Scan0;
//可读写的方式锁定全部容器
BitmapData data2 = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
//得到首地址
IntPtr ptr2 = data2.Scan0;
//计算24位图像的字节数
int bytes = curBitmap.Width * curBitmap.Height * 3;
//定义位图数组
byte[] grayValues = new byte[bytes];
//复制被锁定的图像到该数组
System.Runtime.InteropServices.Marshal.Copy(ptr1, grayValues, 0, bytes);
//同上
int byte8 = curBitmap.Width * curBitmap.Height;
byte [] gray8Values=new byte[byte8];
System.Runtime.InteropServices.Marshal.Copy(ptr2, gray8Values, 0, byte8);

for (int i = 0,n=0; i < bytes;i+=3 ,n++)
{
//灰度变换
double colorTemp = grayValues[i + 2] * 0.299 + grayValues[i + 1] * 0.589 + grayValues[i] * 0.114;
//这个是测试用的可以忽略 这是原图24位
grayValues[i + 2] = grayValues[i + 1] = grayValues[i] = (byte)colorTemp;
//将灰度值赋值给 容器 这是新图8位
gray8Values[n] = (byte)colorTemp;
}
//送回数组
System.Runtime.InteropServices.Marshal.Copy( gray8Values, 0,ptr2, byte8);
System.Runtime.InteropServices.Marshal.Copy( grayValues, 0,ptr1, bytes);
//解锁
curBitmap.UnlockBits(data);
bit.UnlockBits(data2);
//调色板
ColorPalette palette = bit.Palette;
//调色板结构体数组 填充
for (int i = 0; i != palette.Entries.Length; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);//(说实话这步为啥这麽做我也不知道)
}
bit.Palette = palette;

String path2 = System.AppDomain.CurrentDomain.BaseDirectory;
String str = path2 + "bb.bmp";
Bitmap b = bit.Clone(new Rectangle(0, 0, bit.Width, bit.Height), PixelFormat.Format8bppIndexed);
b.Save(@"H:\C#\bb.bmp", ImageFormat.Bmp);// b.Save(@str, ImageFormat.Jpeg);
b.Dispose();
bit.Dispose();
Invalidate();

}

}

利用红色的代码保存的BMP图片是8位的没错,是我想要的,但如果改成保存时JPEG的话图片就会是24位的了,有没有人知道这是为什么?不过这不是重点,关键是如果我把红色的代码注释掉,改用下面的代码

private void save_Click(object sender, EventArgs e)
{
if(curBitmap == null)
{
return;
}
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存为";
saveDlg.OverwritePrompt = true;
saveDlg.Filter =
"BMP文件 (*.bmp) | *.bmp|" +
"Gif文件 (*.gif) | *.gif|" +
"JPEG文件 (*.jpg) | *.jpg|" +
"PNG文件 (*.png) | *.png";
saveDlg.ShowHelp = true;
if(saveDlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveDlg.FileName;
string strFilExtn = fileName.Remove(0, fileName.Length - 3);
switch (strFilExtn)
{
case "bmp":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpg":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "tif":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
case "png":
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
break;
}
}
}

如果用这段代码的话,不管是保存为BMP还是JPEG还是PNG都是24位的了,其他的格式不管了,但是BMP格式的话,我想执行完函数后,在我的屏幕上显示的应该就是8位的吧,那为什么上面的红色代码保存的是8位的,而这段代码保存的却是24位的呢?哪位大大能帮我一下啊!
...全文
156 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
结婚兔 2011-02-14
  • 打赏
  • 举报
回复
我也遇到了这样的问题,想把8位灰度TIF转成8位回复JPG,希望老手解答
woyaoshi 2010-07-04
  • 打赏
  • 举报
回复
我想自己选择,而不是一开始就顶好了的

110,541

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧