使用指针把图片转换成二维数组

/// <summary> /// 使用指针扫描图片,返回存放像素值的二维数组(高,宽) /// </summary> /// <param name="bmp">预处理图片</param> /// <param name="grizzly">是否为灰色图片</param> /// <returns>返回存放像素值的二维数组(高,宽)</returns> public string[,] LeveScan(Bitmap bmp, bool grizzly) { //创建图像 (防止LockBits方法出错,多加了一步) Bitmap bmNew = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmNew); g.DrawImage(bmp, new Point(0, 0)); g.Dispose(); string[,] temp = new string[bmp.Height, bmp.Width]; //获取图像的BitmapData对像 BitmapData data = bmNew.LockBits(new Rectangle(0, 0, bmNew.Width, bmNew.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //循环处理 unsafe { byte* ptr = (byte*)(data.Scan0); for (int i = 0; i < data.Height; i++) { for (int j = 0; j < data.Width; j++) { if (grizzly) { temp[i, j] = ptr[2].ToString(); //红 } else { temp[i, j] = ptr[2].ToString() + "-"; //红 temp[i, j] += ptr[1].ToString() + "-"; //绿 temp[i, j] += ptr[0].ToString(); //蓝 } ptr += 3; } ptr += data.Stride - data.Width * 3; } } bmNew.UnlockBits(data); return temp; }

你可能感兴趣的:(String,byte)