Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. [GetColorAtPoint]System.ArgumentException: Parameter is not valid.
  2. at System.Drawing.Graphics.GetHdc()
  3. at SGetColorAtPoint(Point p, IntPtr hWnd)
  4.  
  5. [DllImport("gdi32.dll")]
  6. private static extern int BitBlt(IntPtr srchDC, int srcX, int srcY, int srcW, int srcH, IntPtr desthDC, int destX, int destY, int op);
  7. [DllImport("user32.dll")]
  8. static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
  9. public static Color GetColorAtPoint(Point p, IntPtr hWnd)
  10. {
  11. try
  12. {
  13. Bitmap screenPixel = new Bitmap(1, 1);
  14. Color c = System.Drawing.Color.Black;
  15.  
  16. //method to test
  17. if (IsWindow(hWnd))
  18. using (Graphics gdest = Graphics.FromImage(screenPixel))
  19. {
  20. if (IsWindow(hWnd))
  21. {
  22. using (Graphics gsrc = Graphics.FromHwnd(hWnd))
  23. {
  24. IntPtr hSrcDC = gsrc.GetHdc();
  25. IntPtr hDestDC = gdest.GetHdc();
  26. int retval = BitBlt(hDestDC, 0, 0, 1, 1, hSrcDC, p.X, p.Y, (int)CopyPixelOperation.SourceCopy);
  27. gdest.ReleaseHdc();
  28. gsrc.ReleaseHdc();
  29. DeleteDC(hSrcDC);
  30. DeleteDC(hDestDC);
  31.  
  32. }
  33. }
  34. }
  35. c = screenPixel.GetPixel(0, 0);
  36. return c;
  37.  
  38. }
  39. catch (Exception ex)
  40. {
  41. Console.WriteLine("[GetColorAtPoint]"+ex.ToString());
  42. return System.Drawing.Color.Black;
  43. }
  44. }
  45.  
  46. public IntPtr GetHdc() {
  47. IntPtr hdc = IntPtr.Zero;
  48.  
  49. int status = SafeNativeMethods.Gdip.GdipGetDC(new HandleRef(this, this.NativeGraphics), out hdc);
  50.  
  51. if (status != SafeNativeMethods.Gdip.Ok) {
  52. throw SafeNativeMethods.Gdip.StatusException(status);
  53. }
  54.  
  55. this.nativeHdc = hdc; // need to cache the hdc to be able to release with a call to IDeviceContext.ReleaseHdc().
  56.  
  57. return this.nativeHdc;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement