Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [GetColorAtPoint]System.ArgumentException: Parameter is not valid.
- at System.Drawing.Graphics.GetHdc()
- at SGetColorAtPoint(Point p, IntPtr hWnd)
- [DllImport("gdi32.dll")]
- private static extern int BitBlt(IntPtr srchDC, int srcX, int srcY, int srcW, int srcH, IntPtr desthDC, int destX, int destY, int op);
- [DllImport("user32.dll")]
- static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
- public static Color GetColorAtPoint(Point p, IntPtr hWnd)
- {
- try
- {
- Bitmap screenPixel = new Bitmap(1, 1);
- Color c = System.Drawing.Color.Black;
- //method to test
- if (IsWindow(hWnd))
- using (Graphics gdest = Graphics.FromImage(screenPixel))
- {
- if (IsWindow(hWnd))
- {
- using (Graphics gsrc = Graphics.FromHwnd(hWnd))
- {
- IntPtr hSrcDC = gsrc.GetHdc();
- IntPtr hDestDC = gdest.GetHdc();
- int retval = BitBlt(hDestDC, 0, 0, 1, 1, hSrcDC, p.X, p.Y, (int)CopyPixelOperation.SourceCopy);
- gdest.ReleaseHdc();
- gsrc.ReleaseHdc();
- DeleteDC(hSrcDC);
- DeleteDC(hDestDC);
- }
- }
- }
- c = screenPixel.GetPixel(0, 0);
- return c;
- }
- catch (Exception ex)
- {
- Console.WriteLine("[GetColorAtPoint]"+ex.ToString());
- return System.Drawing.Color.Black;
- }
- }
- public IntPtr GetHdc() {
- IntPtr hdc = IntPtr.Zero;
- int status = SafeNativeMethods.Gdip.GdipGetDC(new HandleRef(this, this.NativeGraphics), out hdc);
- if (status != SafeNativeMethods.Gdip.Ok) {
- throw SafeNativeMethods.Gdip.StatusException(status);
- }
- this.nativeHdc = hdc; // need to cache the hdc to be able to release with a call to IDeviceContext.ReleaseHdc().
- return this.nativeHdc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement