• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#获取图像像素

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

第一种方法:

 public static byte[] GetImagePixel(Bitmap img)
        {
            byte[] result = new byte[img.Width*img.Height*3];
            int n = 0;
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                   result[n] =  img.GetPixel(i ,j).R;
                   result[n+1] = img.GetPixel(i, j).G;
                   result[n+2] = img.GetPixel(i, j).B;
                   n += 3;
                }
            }
            return result;
        }

第二种方法:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace ImageCompare
{
    public unsafe class UnsafeBitmap
    {
        Bitmap bitmap;

        // three elements used for MakeGreyUnsafe
        int width;
        BitmapData bitmapData = null;
        Byte* pBase = null;

        public UnsafeBitmap(Bitmap bitmap)
        {
            this.bitmap = new Bitmap(bitmap);
        }

        public UnsafeBitmap(int width, int height)
        {
            this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        }

        public void Dispose()
        {
            bitmap.Dispose();
        }

        public Bitmap Bitmap
        {
            get
            {
                return (bitmap);
            }
        }

        private Point PixelSize
        {
            get
            {
                GraphicsUnit unit = GraphicsUnit.Pixel;
                RectangleF bounds = bitmap.GetBounds(ref unit);

                return new Point((int)bounds.Width, (int)bounds.Height);
            }
        }

        public void LockBitmap()
        {
            GraphicsUnit unit = GraphicsUnit.Pixel;
            RectangleF boundsF = bitmap.GetBounds(ref unit);
            Rectangle bounds = new Rectangle((int)boundsF.X,
          (int)boundsF.Y,
          (int)boundsF.Width,
          (int)boundsF.Height);

            // Figure out the number of bytes in a row
            // This is rounded up to be a multiple of 4
            // bytes, since a scan line in an image must always be a multiple of 4 bytes
            // in length. 
            width = (int)boundsF.Width * sizeof(PixelData);
            if (width % 4 != 0)
            {
                width = 4 * (width / 4 + 1);
            }
            bitmapData =
          bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            pBase = (Byte*)bitmapData.Scan0.ToPointer();
        }

        public PixelData GetPixel(int x, int y)
        {
            PixelData returnValue = *PixelAt(x, y);
            return returnValue;
        }

        public void SetPixel(int x, int y, PixelData colour)
        {
            PixelData* pixel = PixelAt(x, y);
            *pixel = colour;
        }

        public void UnlockBitmap()
        {
            bitmap.UnlockBits(bitmapData);
            bitmapData = null;
            pBase = null;
        }
        public PixelData* PixelAt(int x, int y)
        {
            return (PixelData*)(pBase + y * width + x * sizeof(PixelData));
        }
    }
    public struct PixelData
    {
        public byte blue;
        public byte green;
        public byte red;
    }
}


public static byte[] GetUnsafeImagePixel(Bitmap img)
        {
            byte[] result = new byte[img.Width * img.Height * 3];
            int n = 0;
            UnsafeBitmap unsafeBitmap = new UnsafeBitmap(img);
            unsafeBitmap.LockBitmap();
            int x = 0, y = 0;
            do
            {
                for (x = 0; x < img.Width; x++)
                {
                   PixelData pixel = unsafeBitmap.GetPixel(x,y);
                   result[n] = pixel.red;
                   result[n + 1] = pixel.green;
                   result[n + 2] = pixel.blue;
                   n += 3;
                }
                y++;
            } while (y!=img.Height);
            unsafeBitmap.UnlockBitmap();
            return result;
 
        }

第三种速度最快:

public static unsafe byte[] ConvertTo8Byte(Bitmap img)
{
   byte[] result = new byte[img.Width*img.Height];
    int n = 0;
    BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                   PixelFormat.Format24bppRgb);
    var bp = (byte*)data.Scan0.ToPointer();
   
    for (int i = 0; i != data.Height; i++)
    {
        for (int j = 0; j != data.Width; j++)
        {
            result[n] = bp[i * data.Stride + j * 3];
            n++;
            //0.3R+0.59G+0.11B
            //float value = 0.11F * bp[i * data.Stride + j * 3] + 0.59F * bp[i * data.Stride + j * 3 + 1] +
            //              0.3F * bp[i * data.Stride + j * 3 + 2];
           
        }
    }
    img.UnlockBits(data);
    //img.Dispose();
    return result;
   // ColorPalette palette = bit.Palette;
    //for (int i = 0; i != palette.Entries.Length; i++)
    //{
    //    palette.Entries[i] = Color.FromArgb(i, i, i);
    //}
    //bit.Palette = palette;
    //bit.Save(destFile, ImageFormat.Bmp);
    //img.Dispose();
    //bit.Dispose();
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# 清理非托管资源发布时间:2022-07-13
下一篇:
C#DragDropEffects类与拖动实现发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap