将bitmap文件从32bpp 转化为8bpp

转载: http://www.codeproject.com/Answers/228635/how-to-convert-32bpp-into-8bpp-bitmap#answer1

public static Bitmap ColorToGrayscale(Bitmap bmp)
{
int w = bmp.Width,
h = bmp.Height,
r, ic, oc, bmpStride, outputStride, bytesPerPixel;
PixelFormat pfIn = bmp.PixelFormat;
ColorPalette palette;
Bitmap output;
BitmapData bmpData, outputData;
 
//Create the new bitmap
output = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
 
//Build a grayscale color Palette
palette = output.Palette;
for (int i = 0; i < 256; i++)
{
Color tmp = Color.FromArgb(255, i, i, i);
palette.Entries[ i ] = Color.FromArgb(255, i, i, i);
}
output.Palette = palette;
 
//No need to convert formats if already in 8 bit
if (pfIn == PixelFormat.Format8bppIndexed)
{
output = (Bitmap)bmp.Clone();
 
//Make sure the palette is a grayscale palette and not some other
//8-bit indexed palette
output.Palette = palette;
 
return output;
}
 
//Get the number of bytes per pixel
switch (pfIn)
{
case PixelFormat.Format24bppRgb: bytesPerPixel = 3; break;
case PixelFormat.Format32bppArgb: bytesPerPixel = 4; break;
case PixelFormat.Format32bppRgb: bytesPerPixel = 4; break;
default: throw new InvalidOperationException("Image format not supported");
}
 
//Lock the images
bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,
pfIn);
outputData = output.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed);
bmpStride = bmpData.Stride;
outputStride = outputData.Stride;
 
//Traverse each pixel of the image
unsafe
{
byte* bmpPtr = (byte*)bmpData.Scan0.ToPointer(),
outputPtr = (byte*)outputData.Scan0.ToPointer();
 
if (bytesPerPixel == 3)
{
//Convert the pixel to it's luminance using the formula:
// L = .299*R + .587*G + .114*B
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 3, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
(0.299f * bmpPtr[r * bmpStride + ic] +
0.587f * bmpPtr[r * bmpStride + ic + 1] +
0.114f * bmpPtr[r * bmpStride + ic + 2]);
}
else //bytesPerPixel == 4
{
//Convert the pixel to it's luminance using the formula:
// L = alpha * (.299*R + .587*G + .114*B)
//Note that ic is the input column and oc is the output column
for (r = 0; r < h; r++)
for (ic = oc = 0; oc < w; ic += 4, ++oc)
outputPtr[r * outputStride + oc] = (byte)(int)
((bmpPtr[r * bmpStride + ic] / 255.0f) *
(0.299f * bmpPtr[r * bmpStride + ic + 1] +
0.587f * bmpPtr[r * bmpStride + ic + 2] +
0.114f * bmpPtr[r * bmpStride + ic + 3]));
}
}
 
//Unlock the images
bmp.UnlockBits(bmpData);
output.UnlockBits(outputData);
 
return output;
}

  

转载于:https://www.cnblogs.com/fdyang/archive/2013/05/30/3107619.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值