- /// <summary>
- /// 内存转换法
- /// </summary>
- /// <param name="img"></param>
- public static Bitmap GrayByMemory(Bitmap bmp)
- {
- if (bmp == null)
- return bmp;
- //位图矩形
- Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- //以可读写的方式锁定全部位图像素
- BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
- //得到首地址
- IntPtr ptr = bmpData.Scan0;
- //获取位图的总字节数
- int bytes = bmpData.Stride * bmp.Height;
- //定义出位图数组
- byte[] rgbValues = new byte[bytes];
- //把锁定的像素复制到数组内
- Marshal.Copy(ptr, rgbValues, 0, bytes);
- //灰度化
- double grayColor = 0;
- //逐行扫描
- for (int y = 0; y < bmpData.Height; y++)
- {
- //只处理每行中图像像素的数据,舍弃未用空间
- for (int x = 0; x < bmpData.Width * 3; x += 3)
- {
- grayColor = rgbValues[y * bmpData.Stride + x + 2] * 0.299
- + rgbValues[y * bmpData.Stride + x + 1] * 0.587
- + rgbValues[y * bmpData.Stride + x] * 0.114;
- rgbValues[y * bmpData.Stride + x] =
- rgbValues[y * bmpData.Stride + x + 1] =
- rgbValues[y * bmpData.Stride + x + 2] = (byte)grayColor;
- }
- }
- //把数据复制会位图
- Marshal.Copy(rgbValues, 0, ptr, bytes);
- //解锁位图像素
- bmp.UnlockBits(bmpData);
- return bmp;
- }
- /// <summary>
- /// 图片灰度反转
- /// </summary>
- /// <param name="bmp"></param>
- /// <returns></returns>
- public static Bitmap Reverse(Bitmap bmp)
- {
- if (bmp == null)
- return bmp;
- Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
- int bytes = bmpData.Stride * bmp.Height;
- byte[] rgbValues = new byte[bytes];
- IntPtr ptr = bmpData.Scan0;
- Marshal.Copy(ptr, rgbValues, 0, bytes);
- for (int y = 0; y < bmp.Height; y++)
- for (int x = 0; x < bmp.Width * 3; x ++)
- rgbValues[y * bmpData.Stride + x] = (byte)(255 - rgbValues[y * bmpData.Stride + x]);
- Marshal.Copy(rgbValues, 0, ptr, bytes);
- bmp.UnlockBits(bmpData);
- return bmp;
- }
平移图像
/// <summary>
/// 平移图像
/// </summary>
/// <param name="bmp"></param>
/// <param name="tx"></param>
/// <param name="ty"></param>
/// <returns></returns>
public static Bitmap Translation(Bitmap bmp, int tx, int ty)
{
if (bmp == null || (tx == 0 && ty == 0))
return bmp;
Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
IntPtr ptr = bmpData.Scan0;
Marshal.Copy(ptr, rgbValues, 0, bytes);
byte[] newRgbValues = new byte[bytes];
for (int i = 0; i < bytes; i++)
newRgbValues[i] = 255;
for (int y = 0; y < bmp.Height; y++)
{
if (y + ty >= bmp.Height || y + ty < 0)
continue;
for (int x = 0; x < bmp.Width * 3; x++)
{
if (x + tx * 3 >= bmp.Width * 3 || x + tx * 3 < 0)
continue;
newRgbValues[(y + ty) * bmpData.Stride + x + tx * 3] = rgbValues[y * bmpData.Stride + x];
}
}
Marshal.Copy(newRgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
return bmp;
}- 去杂色,转换为黑白图片
-
- /// <summary>
- /// 去杂色,转换为黑白图片
- /// </summary>
- /// <param name="img"></param>
- public static void Binarization(Bitmap img, int thresholdValue)
- {
- for (int x = 0; x < img.Width; x++)
- {
- for (int y = 0; y < img.Height; y++)
- {
- Color c = img.GetPixel(x, y);
- if (c.R >= thresholdValue)
- img.SetPixel(x, y, Color.FromArgb(255, 255, 255));
- else
- img.SetPixel(x, y, Color.FromArgb(0, 0, 0));
- }
- }
- }