Esempio n. 1
0
        public static Bitmap CaptureCursor(ref int x, ref int y)
        {
            Bitmap bmp;
            IntPtr hicon;

            Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
            Win32Stuff.ICONINFO   icInfo;
            ci.cbSize = Marshal.SizeOf(ci);
            if (Win32Stuff.GetCursorInfo(out ci))
            {
                if (ci.flags == Win32Stuff.CURSOR_SHOWING)
                {
                    hicon = Win32Stuff.CopyIcon(ci.hCursor);
                    if (Win32Stuff.GetIconInfo(hicon, out icInfo))
                    {
                        x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                        y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);

                        Icon ic = Icon.FromHandle(hicon);
                        bmp = ic.ToBitmap();
                        return(bmp);
                    }
                }
            }

            return(null);
        }
Esempio n. 2
0
        private void GetScreenshot(byte[] buffer)
        {
            using (var bitmap = new Bitmap(screenWidth, screenHeight))
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(screenWidth, screenHeight));
                    if (CaptureMouse)
                    {
                        Win32Stuff.CURSORINFO pci;
                        pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32Stuff.CURSORINFO));

                        if (Win32Stuff.GetCursorInfo(out pci))
                        {
                            if (pci.flags == Win32Stuff.CURSOR_SHOWING)
                            {
                                DrawIcon(graphics.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
                                graphics.ReleaseHdc();
                            }
                        }
                    }
                    var bits = bitmap.LockBits(new Rectangle(0, 0, screenWidth, screenHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                    Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
                    bitmap.UnlockBits(bits);

                    // Should also capture the mouse cursor here, but skipping for simplicity
                    // For those who are interested, look at http://www.codeproject.com/Articles/12850/Capturing-the-Desktop-Screen-with-the-Mouse-Cursor
                    //this shit didn't work very well for me. I used that with this https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis
                }
        }
Esempio n. 3
0
        /// <summary>
        /// The capture cursor.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="Bitmap"/>.
        /// </returns>
        public static Bitmap CaptureCursor(ref int x, ref int y)
        {
            Win32Stuff.CURSORINFO cursorInfo = new Win32Stuff.CURSORINFO();
            cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
            if (!Win32Stuff.GetCursorInfo(out cursorInfo))
            {
                return(null);
            }

            if (cursorInfo.flags != Win32Stuff.CURSOR_SHOWING)
            {
                return(null);
            }

            IntPtr hicon = Win32Stuff.CopyIcon(cursorInfo.hCursor);

            if (hicon == IntPtr.Zero)
            {
                return(null);
            }

            Win32Stuff.ICONINFO iconInfo;
            if (!Win32Stuff.GetIconInfo(hicon, out iconInfo))
            {
                return(null);
            }

            x = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
            y = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);

            using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask))
            {
                // Is this a monochrome cursor?
                if (maskBitmap.Height == maskBitmap.Width * 2)
                {
                    Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width);

                    using (Graphics desktopGraphics = Graphics.FromHwnd(Win32Stuff.GetDesktopWindow()))
                    {
                        IntPtr desktopHdc = desktopGraphics.GetHdc();

                        IntPtr maskHdc = GDIStuff.CreateCompatibleDC(desktopHdc);
                        IntPtr oldPtr  = GDIStuff.SelectObject(maskHdc, maskBitmap.GetHbitmap());

                        using (Graphics resultGraphics = Graphics.FromImage(resultBitmap))
                        {
                            IntPtr resultHdc = resultGraphics.GetHdc();

                            // These two operation will result in a black cursor over a white background.
                            // Later in the code, a call to MakeTransparent() will get rid of the white background.
                            GDIStuff.BitBlt(
                                resultHdc,
                                0,
                                0,
                                32,
                                32,
                                maskHdc,
                                0,
                                32,
                                (int)GDIStuff.TernaryRasterOperations.SRCCOPY);
                            GDIStuff.BitBlt(
                                resultHdc,
                                0,
                                0,
                                32,
                                32,
                                maskHdc,
                                0,
                                0,
                                (int)GDIStuff.TernaryRasterOperations.SRCINVERT);

                            resultGraphics.ReleaseHdc(resultHdc);
                            GDIStuff.DeleteDC(resultHdc);
                            GDIStuff.DeleteObject(resultHdc);
                        }

                        IntPtr newPtr = GDIStuff.SelectObject(maskHdc, oldPtr);
                        GDIStuff.DeleteObject(oldPtr);
                        GDIStuff.DeleteObject(newPtr);
                        GDIStuff.DeleteDC(maskHdc);
                        desktopGraphics.ReleaseHdc(desktopHdc);
                        GDIStuff.DeleteDC(desktopHdc);
                    }

                    // Remove the white background from the BitBlt calls,
                    // resulting in a black cursor over a transparent background.
                    resultBitmap.MakeTransparent(Color.White);
                    return(resultBitmap);
                }
            }

            //// Delete the mask, if present.
            // if (iconInfo.hbmMask != IntPtr.Zero)
            // {
            // DeleteObject(iconInfo.hbmMask);
            // }

            //// Delete the color bitmap, if present.
            // if (iconInfo.hbmColor != IntPtr.Zero)
            // {
            // DeleteObject(iconInfo.hbmColor);
            // }
            using (Icon icon = Icon.FromHandle(hicon))
            {
                return(icon.ToBitmap());
            }
        }