c#中如何将32位的bmp转换为8位的bmp。 如何在大概保持原来图像的颜色的情况下,将32位bmp文件改为8位即256色图像。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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位色深的位图了。 Bitmap构造函数像素格式PixelFormat所以你可以用后面两个构造函数,比如Bitmap bmp = new Bitmap(_with, _height, PixelFormat.Format8bppIndexed) 我前段时间做过代码发你看看! 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); C#转换图形的色深http://hi.baidu.com/touchthememory/blog/item/ecaeed3f8048c4ca7c1e716e.html 如何不附加数据库到SQL Server就能读取数据库的内容??? 请教前辈:使用C#调用API函数 小问MVC模式 如何上下移动根节点 怎么读取MSSQL中的数据库实体? C#,dataset 请问提示“该服务器不愿意处理该请求。”是什么错误? 我在用winform开发windows记事本遇到一点问题,请执教,谢谢 请教:我打算用C#来写一个局域网用的即时通讯工具,不知是否合适,再者请说一下QQ的实现原理(限于局域网) C#里面有没有像VB一样的set = nothing方法 很简单的问题:textbox下面画一条线 水晶报表 点击打印按钮打开水晶报表时需要输入Access数据库密码
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Gif);
Image gif = Image.FromStream(ms);
gif.Save("文件2.bmp", ImageFormat.Bmp);//这里保存的就是8位色深的位图了。
Bitmap bmp = new Bitmap(_with, _height, PixelFormat.Format8bppIndexed)
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);
http://hi.baidu.com/touchthememory/blog/item/ecaeed3f8048c4ca7c1e716e.html