欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

读取图片像素的具体实例

程序员文章站 2023-12-19 20:06:22
复制代码 代码如下:   public static short[][] getpixs(bitmap bitmap)   ...

复制代码 代码如下:

   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;
        }      
         

上一篇:

下一篇: