没有合适的资源?快使用搜索试试~ 我知道了~
C#中将图片由24位或者16位图像转换为8位图像
资源推荐
资源详情
资源评论
class To8BitImage
{
/// <summary>
/// Convet图形为索引图形
/// </summary>
/// <param name="p_SourceImage">原始图形</param>
/// <param name="p_PixelFormat">格式 之支持 Format4bppIndexed Format1bppIndexed Format8bppIndexed</param>
/// <param name="p_Dithering">原始图形</param>
/// <returns>图形</returns>
public static Bitmap Convert(Bitmap p_SourceImage, bool p_Dithering)
{
int _Width = p_SourceImage.Width;
int _Height = p_SourceImage.Height;
PixelFormat _SetPixFormat = PixelFormat.Format8bppIndexed;
//创建颜色索引表 为2个颜色
Bitmap _NewBitmap = new Bitmap(_Width, _Height, _SetPixFormat);
ColorPalette _Palette = _NewBitmap.Palette;//LoadPalette(p_SourceImage,256);
//重新获取一个原始图形 色彩设置为24色 R G B
Bitmap _Source = new Bitmap(_Width, _Height, PixelFormat.Format24bppRgb);
Graphics _Graphics = Graphics.FromImage(_Source);
_Graphics.DrawImage(p_SourceImage, 0, 0, _Width, _Height);
_Graphics.Dispose();
{
/// <summary>
/// Convet图形为索引图形
/// </summary>
/// <param name="p_SourceImage">原始图形</param>
/// <param name="p_PixelFormat">格式 之支持 Format4bppIndexed Format1bppIndexed Format8bppIndexed</param>
/// <param name="p_Dithering">原始图形</param>
/// <returns>图形</returns>
public static Bitmap Convert(Bitmap p_SourceImage, bool p_Dithering)
{
int _Width = p_SourceImage.Width;
int _Height = p_SourceImage.Height;
PixelFormat _SetPixFormat = PixelFormat.Format8bppIndexed;
//创建颜色索引表 为2个颜色
Bitmap _NewBitmap = new Bitmap(_Width, _Height, _SetPixFormat);
ColorPalette _Palette = _NewBitmap.Palette;//LoadPalette(p_SourceImage,256);
//重新获取一个原始图形 色彩设置为24色 R G B
Bitmap _Source = new Bitmap(_Width, _Height, PixelFormat.Format24bppRgb);
Graphics _Graphics = Graphics.FromImage(_Source);
_Graphics.DrawImage(p_SourceImage, 0, 0, _Width, _Height);
_Graphics.Dispose();
//获取两个图形的数据
BitmapData _SourceData = _Source.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData _NewData = _NewBitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.WriteOnly, _SetPixFormat);
//复制出图形数据的
byte[] _SourceByte = new byte[_SourceData.Stride * _Height];
byte[] _NewByte = new byte[_NewData.Stride * _Height];
Marshal.Copy(_SourceData.Scan0, _SourceByte, 0, _SourceByte.Length);
Marshal.Copy(_NewData.Scan0, _NewByte, 0, _NewByte.Length);
int _SourceIndex = 0;
int _NewIndex = 0;
//颜色表列
Dictionary<int, byte> _ColorTable = new Dictionary<int, byte>();
Color _SetColor;
for (int i = 0; i != _Height; i++)
{
_NewIndex = i * _NewData.Stride;
_SourceIndex = i * _SourceData.Stride;
for (int z = 0; z != _Width; z++)
{
#region 获取色彩索引
//获取原始图形色彩
_SetColor = Color.FromArgb(0, _SourceByte[_SourceIndex + 2], _SourceByte[_SourceIndex + 1], _SourceByte[_SourceIndex]);
_SourceIndex += 3;
byte _TableIndex = 0;
if (!(_ColorTable.TryGetValue(_SetColor.ToArgb(), out _TableIndex)))
BitmapData _SourceData = _Source.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData _NewData = _NewBitmap.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.WriteOnly, _SetPixFormat);
//复制出图形数据的
byte[] _SourceByte = new byte[_SourceData.Stride * _Height];
byte[] _NewByte = new byte[_NewData.Stride * _Height];
Marshal.Copy(_SourceData.Scan0, _SourceByte, 0, _SourceByte.Length);
Marshal.Copy(_NewData.Scan0, _NewByte, 0, _NewByte.Length);
int _SourceIndex = 0;
int _NewIndex = 0;
//颜色表列
Dictionary<int, byte> _ColorTable = new Dictionary<int, byte>();
Color _SetColor;
for (int i = 0; i != _Height; i++)
{
_NewIndex = i * _NewData.Stride;
_SourceIndex = i * _SourceData.Stride;
for (int z = 0; z != _Width; z++)
{
#region 获取色彩索引
//获取原始图形色彩
_SetColor = Color.FromArgb(0, _SourceByte[_SourceIndex + 2], _SourceByte[_SourceIndex + 1], _SourceByte[_SourceIndex]);
_SourceIndex += 3;
byte _TableIndex = 0;
if (!(_ColorTable.TryGetValue(_SetColor.ToArgb(), out _TableIndex)))
剩余5页未读,继续阅读
资源评论
zkr110
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功