コード例 #1
0
ファイル: JobManager.cs プロジェクト: Goletas/screencapture
        /// <summary>
        /// Captures the screen containing mouse pointer.
        /// </summary>
        /// <param name="includeCursor">
        /// <c>true</c> to capture the mouse pointer; otherwise, <c>false</c>.
        /// </param>
        /// <returns>
        /// The snapshot of the current screen.
        /// </returns>
        private static Bitmap GetCurrentScreenImage(bool includeCursor)
        {
            POINT screenId;
            MONITORINFOEX monitorInfo = new MONITORINFOEX();

            if (NativeMethods.GetCursorPos(out screenId) && NativeMethods.GetMonitorInfoW(NativeMethods.MonitorFromPoint(screenId, NativeMethods.MONITOR_DEFAULTTONEAREST), monitorInfo))
            {
                int width = monitorInfo.rcMonitor.Right - monitorInfo.rcMonitor.Left;
                int height = monitorInfo.rcMonitor.Bottom - monitorInfo.rcMonitor.Top;

                Bitmap outputBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

                using (Graphics outputGraphics = Graphics.FromImage(outputBitmap))
                {
                    using (DeviceContext sourceDC = new DeviceContext(new string(monitorInfo.szDevice)))
                    {
                        IntPtr outputDC = outputGraphics.GetHdc();

                        try
                        {
                            NativeMethods.BitBlt(new HandleRef(outputGraphics, outputDC), width, height, new HandleRef(sourceDC, sourceDC.Handle), 0, 0);

                            if (includeCursor)
                            {
                                CURSORINFO cursorInfo;
                                cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));

                                if (NativeMethods.GetCursorInfo(out cursorInfo) && (cursorInfo.flags == NativeMethods.CURSOR_SHOWING))
                                {
                                    using (IconData icon = new IconData(cursorInfo.hCursor))
                                    {
                                        NativeMethods.DrawItem(new HandleRef(outputGraphics, outputDC), cursorInfo.ptScreenPos.X - monitorInfo.rcMonitor.Left - icon.HotSpotX, cursorInfo.ptScreenPos.Y - monitorInfo.rcMonitor.Top - icon.HotSpotY, new HandleRef(icon, cursorInfo.hCursor));
                                    }
                                }
                            }

                            return outputBitmap;
                        }
                        finally
                        {
                            outputGraphics.ReleaseHdc();
                        }
                    }
                }
            }

            return null;
        }
コード例 #2
0
ファイル: JobManager.cs プロジェクト: Goletas/screencapture
        private static Bitmap GetVirtualScreenImage(bool includeCursor)
        {
            RECT virtualScreenBounds = NativeMethods.GetVirtualScreenBounds();

            int width = virtualScreenBounds.Right - virtualScreenBounds.Left;
            int height = virtualScreenBounds.Bottom - virtualScreenBounds.Top;
            
            Bitmap outputBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            using (Graphics outputGraphics = Graphics.FromImage(outputBitmap))
            {
                outputGraphics.Clear(SystemColors.Desktop);

                using (DeviceContext sourceDC = new DeviceContext())
                {
                    IntPtr outputDC = outputGraphics.GetHdc();

                    try
                    {
                        NativeMethods.BitBlt(new HandleRef(outputGraphics, outputDC), width, height, new HandleRef(sourceDC, sourceDC.Handle), virtualScreenBounds.Left, virtualScreenBounds.Top);

                        if (includeCursor)
                        {
                            CURSORINFO cursorInfo;
                            cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));

                            if (NativeMethods.GetCursorInfo(out cursorInfo) && (cursorInfo.flags == NativeMethods.CURSOR_SHOWING))
                            {
                                using (IconData icon = new IconData(cursorInfo.hCursor))
                                {
                                    NativeMethods.DrawItem(new HandleRef(outputGraphics, outputDC), cursorInfo.ptScreenPos.X - virtualScreenBounds.Left - icon.HotSpotX, cursorInfo.ptScreenPos.Y - virtualScreenBounds.Top - icon.HotSpotY, new HandleRef(icon, cursorInfo.hCursor));
                                }
                            }
                        }
                    }
                    finally
                    {
                        outputGraphics.ReleaseHdc();
                    }
                }
            }

            return outputBitmap;
        }