I am getting error "A generic error occurred in GDI+". I have code below, but it error after some time while loop. I research for all and can not fix it, please help me
public Color GetColorAt(int x, int y)
{
x = x - rect.Left - 7;
y = y - rect.Top - 31;
Color c = Color.Black;
IntPtr hDC = PlatformInvokeUSER32.GetDC(proc.MainWindowHandle);
try
{
if (hDC != IntPtr.Zero)
{
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
if (hMemDC != IntPtr.Zero)
{
IntPtr m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, 1, 1);
if (m_HBitmap != IntPtr.Zero)
{
IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, m_HBitmap);
PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, 1, 1, hDC, x, y, PlatformInvokeGDI32.SRCCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
Bitmap screen = Image.FromHbitmap(m_HBitmap);
c = screen.GetPixel(0, 0);
screen.Dispose();
}
PlatformInvokeGDI32.DeleteObject(m_HBitmap);
PlatformInvokeGDI32.DeleteDC(m_HBitmap);
}
PlatformInvokeGDI32.DeleteDC(hMemDC);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
PlatformInvokeUSER32.ReleaseDC(proc.MainWindowHandle, hDC);
PlatformInvokeGDI32.DeleteDC(hDC);
GC.Collect();
return c;
}
Using call function while loop realtime
public Form1()
{
InitializeComponent();
while (true)
{
Color c = GetColorAt(50, 50);
Thread.Sleep(10);
}
}