【图片二值化处理,以及byte[] 与bitmap互相转化问题】

1.byte与bitmap相互转换

					//将byte流转换为bitmap
                    byte[] signature = item.ToArray();
                    MemoryStream ms1 = new MemoryStream(signature);
                    Bitmap bm = (Bitmap)Image.FromStream(ms1);
                    ms1.Close();
                    //图片二值化处理方法
                    var nbm = binaryzation(bm);
                    MemoryStream ms = new MemoryStream();
                    nbm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    var nSignature = ms.ToArray();  
                    ms.Close();

2.图片二值化处理

		public static Bitmap binaryzation(Bitmap bitmap)
        {
            //得到图形的宽度和长度  
            int width = bitmap.Width;
            int height = bitmap.Height;
            //创建二值化图像
            Bitmap binarymap = new Bitmap(bitmap);
            //依次循环,对图像的像素进行处理
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    //得到当前像素
                    int col = binarymap.GetPixel(i, j).ToArgb();
                    var alpha = (int)(col & 0xFF000000);
                    //得到图像的像素RGB  blue0-8位,green9-16位,red17-24位
                    int red = (col & 0x00FF0000) >> 16;
                    int green = (col & 0x0000FF00) >> 8;
                    int blue = (col & 0x000000FF);
                    // 用灰度值公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB  
                    int gray = (int)((float)red * 0.3 + (float)green * 0.59 +
                            (float)blue * 0.11);
                    //对图像进行二值化处理  
                    if (gray <= 100)
                    {
                        gray = 0;
                    }
                    else
                    {
                        gray = 255;
                    }
                    // 新的ARGB  
                    int newColor = alpha | (gray << 16) | (gray << 8) | gray;
                    //设置新图像的当前像素值
                     binarymap.SetPixel(i, j, Color.FromArgb(newColor));
                }
            }
            return binarymap;
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bitmap转换为byte数组可以使用Bitmap类的LockBits方法和Marshal类的Copy方法。下面是一个简单的示例代码: ```csharp // 将Bitmap转换为byte数组 public static byte[] BitmapToByteArray(Bitmap bmp) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] data = new byte[bytes]; Marshal.Copy(ptr, data, 0, bytes); bmp.UnlockBits(bmpData); return data; } ``` 在上述示例中,首先获取Bitmap对象的像素数据,然后通过LockBits方法获取一个BitmapData对象进行读取和写入操作。接着使用Marshal类的Copy方法将BitmapData对象中的像素数据复制到byte数组中。最后通过UnlockBits方法释放BitmapData对象的资源,并返回byte数组。 需要注意的是,在使用LockBits方法时,需要指定Bitmap的像素格式和锁定模式。如果Bitmap的像素格式不是8位灰度图像,则需要进行像素格式转换。在复制像素数据时,需要考虑BitmapData对象的Stride属性,它表示Bitmap一行像素数据所占的字节数,可能包含一些填充字节。在使用Marshal.Copy方法时需要将bytes参数设置为Stride x Height。 如果Bitmap对象的像素格式为8位灰度图像,则可以直接将BitmapData对象的Scan0指针转换为byte数组,然后复制像素数据。下面是一个简单的示例代码: ```csharp // 将Bitmap转换为byte数组(8位灰度图像) public static byte[] BitmapToByteArray(Bitmap bmp) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); byte[] data = new byte[bmpData.Stride * bmp.Height]; Marshal.Copy(bmpData.Scan0, data, 0, data.Length); bmp.UnlockBits(bmpData); return data; } ``` 在上述示例中,通过将BitmapData对象的Scan0指针转换为byte数组,避免了使用Marshal类的Copy方法进行复制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值