当前位置: 移动技术网 > IT编程>开发语言>c# > 读取图片像素的具体实例

读取图片像素的具体实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

   public static short[][] getpixs(bitmap bitmap)
        {
            int height = bitmap.height;
            int width = bitmap.width;
            byte tempb, tempg, tempr;
            short[][] sporigindata = new short[height][];
            for (int i = 0; i < height; i++)
            {
                sporigindata[i] = new short[width];
            }

            bitmapdata dataout = bitmap.lockbits(new rectangle(0, 0, width, height), imagelockmode.readwrite, pixelformat.format24bpprgb);
            int offset = dataout.stride - dataout.width * 3;
            try
            {
                unsafe
                {
                    byte* pout = (byte*)(dataout.scan0.topointer());

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            tempb = pout[0];
                            tempg = pout[1];
                            tempr = pout[2];
                            double data=0.31 * tempr + 0.59 * tempg + 0.11 * tempb;
                            if (data > 255)
                                sporigindata[y][x] = 255;
                            else
                                if (data < 0)
                                    sporigindata[y][x] = 0;
                                else
                                    sporigindata[y][x] = (short)data;
                            pout += 3;
                        }
                        pout += offset;
                    }
                    bitmap.unlockbits(dataout);
                }
            }
            catch
            {
            }
            return sporigindata;
        }      
         

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网