读取图片像素的具体实例

C#读取图片像素的具体实例,需要的朋友可以参考一下

复制代码 代码如下:

   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             {
                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                     {
                        for (int x = 0; 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;
        }      
         

以上就是读取图片像素的具体实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程