zoukankan      html  css  js  c++  java
  • C# Bitmap GetPixel 效率太低,太慢的替代方法

    新建类 

        public class DirectBitmap : IDisposable
        {
            public Bitmap Bitmap { get; private set; }
            int[] Bits = null;
            public int Height { get; private set; }
            public int Width { get; private set; }
    
            protected GCHandle BitsHandle { get; private set; }
    
            public DirectBitmap(int width, int height)
            {
                Width = width;
                Height = height;
                Bits = new int[width * height];
                BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
                Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
            }
    
            public void SetPixel(int x, int y, Color colour)
            {
                SetPixelRGB(x, y, colour.ToArgb());
            }
    
            public void SetPixelRGB(int x, int y, int colour)
            {
                Bits[x + (y * Width)] = colour;
            }
    
            public Color GetPixel(int x, int y)
            {
                return Color.FromArgb(GetPixelRGB(x, y));
            }
    
            public int GetPixelRGB(int x, int y)
            {
                return Bits[x + (y * Width)];
            }
    
            private bool Disposed = false;
    
            public void Dispose()
            {
                if (Disposed)
                    return;
                Disposed = true;
                Bitmap.Dispose();
                BitsHandle.Free();
            }
        }

    使用时:

    声明DirectBitmap实例,访问DirectBitmap的Bitmap即可。 使用DirectBitmap的GetPixel方法来获取颜色 

                DirectBitmap image = new DirectBitmap(rc.Right - rc.Left, rc.Bottom - rc.Top);
                using (Graphics gp = Graphics.FromImage(image.Bitmap))
                {
                    IntPtr dc = gp.GetHdc();
                    FormHelper.PrintWindow(handle, dc, 0);
                    gp.ReleaseHdc();
                }
  • 相关阅读:
    csuOJ啊 1553
    Codeforces 111B【看看自己和别人在代码能力上的差距!】
    hdu1849
    hdu 1847
    校队训练赛,同时也是HDU4497(数论:素数分解+组合数学)
    POJ 2356 (抽屉原理)
    线段树总结一【转】
    训练赛(1---5)D
    训练赛(1---5)A
    HDU1556 【树状数组】(改段求点)
  • 原文地址:https://www.cnblogs.com/xyz0835/p/9277582.html
Copyright © 2011-2022 走看看