0

I am building a project to get the color of certain screen pixels. The project already works however I would like to know if there's a better and faster way to do it. here's a part of the code I'm using: In here I want to get the colors of certain pixels starting from pixel (265,400) and moving 10 steps horizontally and 9 vertically , the difference between 2 steps is 40 pixels .

     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; }
         }

And here is the GetDesktop() function of the class WIN32APICall

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;
    }


}

1 Answers1

0

It's not clear from the snippet, but if you're NOT interested in the entire desktop, and only want a smaller portion, then consider using Graphics.CopyFromScreen(). Don't forget to shift your loops so they start at 0 (zero):

        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 ..
            }
        }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thank you, I'll give it a try but is this method faster than the one I'm using ? I really care about the speed of execution in here. – user2386390 May 17 '13 at 08:05
  • Using the APIs is probably the fastest method; mainly my point is don't grab the entire screen if you're only interested in a tiny portion. That'll speed things up a lot. – Idle_Mind May 17 '13 at 12:34