将彩色的图片转换成黑白有很多种方法:
1。红色转换灰度
Bitmap desc = new Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
sourceptr
+=
2
;
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
for
(
int
x
=
0
; x
<
source.Height; x
++
)
{
for
(
int
y
=
0
; y
<
source.Width; y
++
)
{
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
sourceptr
+=
3
;
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
2。绿色转换灰度
Bitmap desc
=
new
Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
sourceptr
+=
1
;
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
for
(
int
x
=
0
; x
<
source.Height; x
++
)
{
for
(
int
y
=
0
; y
<
source.Width; y
++
)
{
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
sourceptr
+=
3
;
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
3。蓝色转换灰度
Bitmap desc = new Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
for
(
int
x
=
0
; x
<
source.Height; x
++
)
{
for
(
int
y
=
0
; y
<
source.Width; y
++
)
{
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
*
(descptr
++
)
=
*
sourceptr;
sourceptr
+=
3
;
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
4。平均转换灰度
Bitmap desc
=
new
Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
for
(
int
x
=
0
; x
<
source.Height; x
++
)
{
for
(
int
y
=
0
; y
<
source.Width; y
++
)
{
*
(descptr)
=
*
(descptr
+
1
)
=
*
(descptr
+
2
)
=
(
byte
)((
*
sourceptr
+
*
(sourceptr
+
1
)
+
*
(sourceptr
+
2
))
/
3
);
descptr
+=
3
;
sourceptr
+=
3
;
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);
5。加权平均转换灰度
Bitmap desc
=
new
Bitmap(source.Width, source.Height);
BitmapData sourcedata
=
source.LockBits(
new
Rectangle(
0
,
0
, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData descdata
=
desc.LockBits(
new
Rectangle(
0
,
0
, desc.Width, desc.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte
*
sourceptr
=
(
byte
*
)sourcedata.Scan0;
//
B,G,R
byte
*
descptr
=
(
byte
*
)descdata.Scan0;
for
(
int
x
=
0
; x
<
source.Height; x
++
)
{
for
(
int
y
=
0
; y
<
source.Width; y
++
)
{
*
(descptr)
=
*
(descptr
+
1
)
=
*
(descptr
+
2
)
=
(
byte
)(
*
sourceptr
*
0.11
+
*
(sourceptr
+
1
)
*
0.58
+
*
(sourceptr
+
2
)
*
0.31
);
descptr
+=
3
;
sourceptr
+=
3
;
}
sourceptr
+=
sourcedata.Stride
-
source.Width
*
3
;
descptr
+=
descdata.Stride
-
desc.Width
*
3
;
}
}
source.UnlockBits(sourcedata);
desc.UnlockBits(descdata);