【问题标题】:Get all screen pixels colors with C# [duplicate]使用C#获取所有屏幕像素颜色[重复]
【发布时间】:2013-05-10 05:57:16
【问题描述】:

我正在构建一个项目来获取某些屏幕像素的颜色。该项目已经运行,但是我想知道是否有更好更快的方法来做到这一点。这是我正在使用的代码的一部分: 在这里,我想获取某些像素的颜色,从像素 (265,400) 开始,水平移动 10 步,垂直移动 9 步,两步之间的差是 40 像素。

     myBitmap = Win32APICall.GetDesktop();
     int DX = 0 ; int DY=0; int index = 1;
     MyPoint[,] MyPoint_Array = new MyPoint[10,9]
     for(int y = 400;y<750;y+=40)
       {
           for(int x=266;x<650;x+=40)
           {
               Color MyColor = myBitmap.GetPixel(x,y);
               MyPoint M = new MyPoint(index,MyColor.ToArgb());

                MyPoint_Array[DX, DY] = M;
                index++;
                    if (DX < 9)
                    { DX++; }
                    else
                    { DX = 0; }
             }

                if (DY < 8)
                { DY++; }
                else
                { DY = 0; }
         }

这里是WIN32APICall类的GetDesktop()函数

class Win32APICall
{
    [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
    public static extern IntPtr DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    public static extern IntPtr DeleteObject(IntPtr hObject);

    [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
    public static extern bool BitBlt(IntPtr hdcDest, int nXDest,
        int nYDest, int nWidth, int nHeight, IntPtr hdcSrc,
        int nXSrc, int nYSrc, int dwRop);

    [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
        int nWidth, int nHeight);

    [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
    public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobjBmp);

    [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll", EntryPoint = "GetDC")]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern int GetSystemMetrics(int nIndex);

    [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

    public static Bitmap GetDesktop()
    {
        int screenX;
        int screenY;
        IntPtr hBmp;
        IntPtr hdcScreen = GetDC(GetDesktopWindow());
        IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);

        screenX = GetSystemMetrics(0);
        screenY = GetSystemMetrics(1);
        hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);

        if (hBmp != IntPtr.Zero)
        {
            IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp);
            BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376);

            SelectObject(hdcCompatible, hOldBmp);
            DeleteDC(hdcCompatible);
            ReleaseDC(GetDesktopWindow(), hdcScreen);

            Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);

            DeleteObject(hBmp);
            GC.Collect();

            return bmp;
        }

        return null;
    }


}

【问题讨论】:

    标签: c# colors screen capture pixels


    【解决方案1】:

    从 sn-p 中不清楚,但如果您对整个桌面不感兴趣,并且只想要更小的部分,那么请考虑使用 Graphics.CopyFromScreen()。不要忘记移动循环,使它们从 0(零)开始:

            Point upperLeftSouce = new Point(266, 400);
            Size blockRegionSize = new Size(385, 351);
            Bitmap bmp = new Bitmap(blockRegionSize.Width, blockRegionSize.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(upperLeftSouce, new Point(0, 0), bmp.Size);
            }
    
            for (int y = 0; y < bmp.Height; y += 40)
            {
                for (int x = 0; x < bmp.Width; x += 40)
                {
                    // ... code ..
                }
            }
    

    【讨论】:

    • 谢谢,我试试看,这个方法比我用的快吗?我真的很关心这里的执行速度。
    • 使用 API 可能是最快的方法;主要是我的观点是,如果您只对一小部分感兴趣,请不要抓住整个屏幕。这会大大加快速度。
    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多