C# 图像快速转化成byte[]和计算像素值

 public static unsafe byte[] ConvertTo8Byte(Bitmap img)
        {
            byte[] result = new byte[img.Width * img.Height];
            int n = 0;
            BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                           PixelFormat.Format24bppRgb);
            var bp = (byte*)data.Scan0.ToPointer();

            for (int i = 0; i != data.Height; i++)
            {
                for (int j = 0; j != data.Width; j++)
                {
                    result[n] = bp[i * data.Stride + j * 3];
                    n++;
                    //0.3R+0.59G+0.11B
                    float value = 0.11F * bp[i * data.Stride + j * 3] + 0.59F * bp[i * data.Stride + j * 3 + 1] +
                                  0.3F * bp[i * data.Stride + j * 3 + 2];

                }
            }
            img.UnlockBits(data);
            //img.Dispose();
            return result;
            // ColorPalette palette = bit.Palette;
            //for (int i = 0; i != palette.Entries.Length; i++)
            //{
            //    palette.Entries[i] = Color.FromArgb(i, i, i);
            //}
            //bit.Palette = palette;
            //bit.Save(destFile, ImageFormat.Bmp);
            //img.Dispose();
            //bit.Dispose();
        }

 

posted @ 2013-08-15 23:19  louiskoo  阅读(3406)  评论(0编辑  收藏  举报