System.ArgumentException System.Drawing.Graphics.GetHdc()

56 views Asked by At

I'm a low skilled hobby programmer and I wrote a program with a function to get the color of a pixel on the screen. The following Method is not wrote by myself. My Program is running for about 20 minutes without any Problems and use the Fuction to get the Color of the Pixel a lot (hundreds or thousends of calls). But after round about 20 Minutes my Program crashes with the following error (sorry for german / full Errors see below):

Anwendung: MyApp.exe
Frameworkversion: v4.0.30319
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.

Ausnahmeinformationen: System.ArgumentException
   bei System.Drawing.Graphics.GetHdc()
   bei MyApp.ColorChecker.GetColorAtCheckPoint(Int32, Int32)
   bei MyApp.MyAppMain+<Run>d__4.MoveNext()
   ....

My Code:

Color colorAtCheckpoint = ColorChecker.GetColorAtCheckPoint(pos.X,pos.Y);

This Code is copy / paste: (but works like a charme most of the time)

public static Color GetColorAtCheckPoint(int checkpointX, int checkpointY)
       {
           screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
           using (Graphics gdest = Graphics.FromImage(screenPixel))
           {
               using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
               {
                   IntPtr hSrcDC = gsrc.GetHdc();
                   IntPtr hDC = gdest.GetHdc();
                   int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, checkpointX, checkpointY, (int)CopyPixelOperation.SourceCopy);
                   gdest.ReleaseHdc();
                   gsrc.ReleaseHdc();
               }
           }
           //MessageBox.Show(new Form() { TopMost = true }, $"Color Found: " + screenPixel.GetPixel(0, 0));
           return screenPixel.GetPixel(0, 0);
       }

Is there any Problem with the copied code, or is there a possible bug in system.drawing?

Thanks for any help!

Error01:

Anwendung: MyApp.exe
Frameworkversion: v4.0.30319
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.
Ausnahmeinformationen: System.ArgumentException
   bei System.Drawing.Graphics.GetHdc()
   bei MyApp.ColorChecker.GetColorAtCheckPoint(Int32, Int32)
   bei MyApp.MyAppMain+<Run>d__4.MoveNext()
   bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
   bei System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   bei MyApp.MainWindow+<Start>d__15.MoveNext()
   bei System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0(System.Object)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   bei System.Windows.Threading.DispatcherOperation.InvokeImpl()
   bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   bei MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)
   bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   bei MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object)
   bei System.Windows.Threading.DispatcherOperation.Invoke()
   bei System.Windows.Threading.Dispatcher.ProcessQueue()
   bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   bei MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   bei MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   bei System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   bei System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
   bei System.Windows.Application.RunDispatcher(System.Object)
   bei System.Windows.Application.RunInternal(System.Windows.Window)
   bei System.Windows.Application.Run(System.Windows.Window)
   bei System.Windows.Application.Run()
   bei MyApp.App.Main()

Error02:

Name der fehlerhaften Anwendung: MyApp.exe, Version: 1.0.0.0, Zeitstempel: 0x84c44dec
Name des fehlerhaften Moduls: KERNELBASE.dll, Version: 10.0.19041.3570, Zeitstempel: 0xfaa05682
Ausnahmecode: 0xe0434352
Fehleroffset: 0x0013d982
ID des fehlerhaften Prozesses: 0x4fc
Startzeit der fehlerhaften Anwendung: 0x01da07e229d3bede
Pfad der fehlerhaften Anwendung: C:\Program Files\MyApp\MyApp.exe
Pfad des fehlerhaften Moduls: C:\Windows\System32\KERNELBASE.dll
Berichtskennung: 099f4e2d-fce9-4d2f-8678-5a54bdffa50c
Vollständiger Name des fehlerhaften Pakets: 
0

There are 0 answers