码迷,mamicode.com
首页 > Windows程序 > 详细

C#getPixel和内存法读取灰度图信息

时间:2017-12-21 11:52:32      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:src   post   bsp   unlock   灰度   data   nbsp   safe   readwrite   

getPixel方法:

        private int getPixels(Bitmap bmpobj)
        {
            int[,] data = new int[1920, 1200];
            int max = 0;
            for (int i = 0; i < bmpobj.Height; i++)
            {
                for (int j = 0; j < bmpobj.Width; j++)
                {
                    Color b = bmpobj.GetPixel(j, i);
                    if (max < b.R)
                    {
                        max = b.R;
                    }
                }
            }
            return max;
        }

 

内存法:

private int GetImgInfo(Bitmap a)
        {
            Bitmap src = new Bitmap(a);
            int w = src.Width;
            int h = src.Height;
            int max = 0;
            BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            unsafe
            {
                byte* p = (byte*)srcData.Scan0;
                int gray = 0;
                for (int j = 1; j < h - 1; j++)
                {
                    for (int i = 1; i < w - 1; i++)
                    {
                        gray = p[0] ;//+= (p[0] + p[1] + p[2]) / 3;
                        p += 4;
                        if (max < gray)
                        {
                            max = gray;
                        }
                    }
                    p += srcData.Stride - w * 4;
                }
                src.UnlockBits(srcData);
                return  max ;
            }
        }

 

C#getPixel和内存法读取灰度图信息

标签:src   post   bsp   unlock   灰度   data   nbsp   safe   readwrite   

原文地址:http://www.cnblogs.com/zhayunjia/p/8078606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!