// 锁住数据.  
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            System.Drawing.Imaging.BitmapData bitmapData =
                bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bitmap.PixelFormat);
            IntPtr ptr = bitmapData.Scan0;
            int bytes = bitmap.Width * bitmap.Height * 3;
            byte[] rgbValues = new byte[bytes];            // 遍历数据.  
            //bitmap.Palette.Entries[y * bitmapData.Stride + x]   //这个是灰度图像的定位方法
            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                   byte bColorIndex = *(bitmapData.Scan0 + y * bitmapData.Stride + x);
                    Color currentColor = bitmap.Palette.Entries[bColorIndex];
                }
            }
            bitmap.UnlockBits(bitmapData);

解决方案 »

  1.   

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);少了一句
      

  2.   

           private void FindLines(Bitmap bitmap)
            {            // 锁住数据.  
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                System.Drawing.Imaging.BitmapData bitmapData =
                    bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                IntPtr ptr = bitmapData.Scan0;
                int bytes = bitmap.Width * bitmap.Height * 3;
                byte[] rgbValues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);            // 遍历数据.  
                //bitmap.Palette.Entries[y * bitmapData.Stride + x]   //这个是灰度图像的定位方法
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        byte bColorIndex = rgbValues[y * bitmapData.Stride + x];
                        Color currentColor = bitmap.Palette.Entries[bColorIndex];                }
                }
                bitmap.UnlockBits(bitmapData);
            }
      

  3.   

    Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 
                System.Drawing.Imaging.BitmapData bitmapData = 
                    bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, 
                    bitmap.PixelFormat); 
                byte* p = bitmapData.Scan0;             for (int y = 0; y < bitmap.Height; y++) 
                { 
                    for (int x = 0; x < bitmap.Width; x++) 
                    { 
                        //假设图像的像素位是1个字节,8位,且使用了调色板
                        byte bColorIndex = *(p + y * bitmapData.Stride + x); 
                        Color currentColor = bitmap.Palette.Entries[bColorIndex]; 
                    } 
                } 
                bitmap.UnlockBits(bitmapData);这样写试试
      

  4.   

    你这样写也可以,我提供的那种方法是用指针直接访问的,要加unsafe标记
      

  5.   


            private unsafe void FindLines()
            {
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
                bitmap = ScreenHelper.GetGrayBmp(bitmap);
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                byte* p = (byte*)bitmapData.Scan0;            for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        //假设图像的像素位是1个字节,8位,且使用了调色板 
                        byte bColorIndex = *(p + y * bitmapData.Stride + x);
                        Color currentColor = bitmap.Palette.Entries[bColorIndex];
                    }
                }            bitmap.UnlockBits(bitmapData);
            }
      

  6.   

    byte* p = bitmapData.Scan0; 马虎大意了,这一句要改成byte* p = (byte*)bitmapData.Scan0; 
      

  7.   

    private unsafe void FindLines()
            {
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
                bitmap = ScreenHelper.GetGrayBmp(bitmap);
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);                    Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
                Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length);
                             for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                                            
                       Color currentColor = ??                }
                }            bitmap.UnlockBits(bitmapData);
            }
      

  8.   

            private unsafe void FindLines()
            {
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
                bitmap = ScreenHelper.GetGrayBmp(bitmap);
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);                    Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height );
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height];
                Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length);            byte* p = (byte*)bitmapData.Scan0; 
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        //假设图像的像素位是1个字节,8位,且使用了调色板                      
                        byte bColorIndex = *(p + y * bitmapData.Stride + x);
                        Color currentColor = bitmap.Palette.Entries[bColorIndex]; 
                    }
                }            bitmap.UnlockBits(bitmapData);
            }
      

  9.   

         private unsafe void FindLines() 
            { 
                
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect); 
                //查下看这个地方有没有问题
                bitmap = ScreenHelper.GetGrayBmp(bitmap); 
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);                    Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height ); 
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);             //去掉这两句就行了
                //byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height]; 
                //Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length);             byte* p = (byte*)bitmapData.Scan0; 
                for (int y = 0; y < bitmap.Height; y++) 
                { 
                    for (int x = 0; x < bitmap.Width; x++) 
                    { 
                        //假设图像的像素位是1个字节,8位,且使用了调色板                      
                           byte bColorIndex = *(p + y * bitmapData.Stride + x); 
                        Color currentColor = bitmap.Palette.Entries[bColorIndex]; 
                    } 
                }             bitmap.UnlockBits(bitmapData); 
            }
      

  10.   

    private unsafe void FindLines() 
            { 
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect); 
                //看看这里有没有问题,如果PixFormat不对也会出现问题
                bitmap = ScreenHelper.GetGrayBmp(bitmap); 
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);                    Rectangle bitmapRect = new Rectangle(0, 0, rect.Width , rect.Height ); 
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);             //这两句没用了,去掉去
                //byte[] bytes = new byte[bitmapData.Stride * bitmapData.Height]; 
                //Marshal.Copy(bitmapData.Scan0, bytes, 0, bytes.Length);             byte* p = (byte*)bitmapData.Scan0; 
                for (int y = 0; y < bitmap.Height; y++) 
                { 
                    for (int x = 0; x < bitmap.Width; x++) 
                    { 
                        //假设图像的像素位是1个字节,8位,且使用了调色板                      
                        byte bColorIndex = *(p + y * bitmapData.Stride + x); 
                        Color currentColor = bitmap.Palette.Entries[bColorIndex]; 
                    } 
                }             bitmap.UnlockBits(bitmapData); 
            }用这个方法试试看吧
      

  11.   


            private unsafe void FindLines()
            {
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
                //查下看这个地方有没有问题 
                bitmap = ScreenHelper.GetGrayBmp(bitmap);
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);            Rectangle bitmapRect = new Rectangle(0, 0, rect.Width, rect.Height);
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
                byte* p = (byte*)bitmapData.Scan0;
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                                             
                        byte bColorIndex = *(p + y * bitmapData.Stride + x);
                        Color currentColor = bitmap.Palette.Entries[bColorIndex];
                    }
                }            bitmap.UnlockBits(bitmapData);         }
      

  12.   

            private unsafe void FindLines()
            {
                Bitmap bitmap = ScreenHelper.CaptrueScreen(ScreenHelper.FullScreenRect);
                bitmap = ScreenHelper.GetGrayBmp(bitmap);
                Color[] colors = bitmap.Palette.Entries;
                Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);            Rectangle bitmapRect = new Rectangle(0, 0, rect.Width, rect.Height);
                BitmapData bitmapData = bitmap.LockBits(bitmapRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);            byte* p = (byte*)bitmapData.Scan0;
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        byte bColorIndex = *(p + y * bitmapData.Stride + x);
                        
                        Color currentColor = colors[bColorIndex];
                    }
                }            bitmap.UnlockBits(bitmapData);
                bitmap.Dispose();
            }这样写经调试正常运行