コード例 #1
0
        private Image CaptureWindow(CaptureMode mode)
        {
            Image     img     = null;
            IntPtr    handle  = IntPtr.Zero;
            Rectangle monitor = Rectangle.Empty;

            switch (mode)
            {
            case CaptureMode.FullScreen:
                handle = WindowNative.GetDesktopWindow();
                if (CaptureSetting.FullScreenMode == FullScreenMode.MainMonitor)
                {
                    monitor = Screen.PrimaryScreen.Bounds;
                }
                else if (CaptureSetting.FullScreenMode == FullScreenMode.ActiveMonitor)
                {
                    monitor = Screen.GetBounds(Cursor.Position);
                }
                else if (CaptureSetting.FullScreenMode == FullScreenMode.AllMonitor)
                {
                    monitor = SystemInformation.VirtualScreen;
                }
                img = CaptureWindow(handle, monitor.X, monitor.Y, monitor.Width, monitor.Height);
                break;

            case CaptureMode.ActiveProcess:
                handle = WindowNative.GetForegroundWindow();
                img    = CaptureWindow(handle, 0, 0, 0, 0);
                break;

            case CaptureMode.Region:
                handle  = WindowNative.GetDesktopWindow();
                monitor = SystemInformation.VirtualScreen;

                using (var tmp = CaptureWindow(handle, monitor.X, monitor.Y, monitor.Width, monitor.Height))
                    using (var dialog = new CaptureBackgroundDialog(tmp))
                    {
                        dialog.TopMost = true;
                        dialog.BringToFront();
                        if (dialog.ShowDialog() != DialogResult.OK)
                        {
                            return(null);
                        }

                        img = dialog.Image;
                    }
                break;

            default: return(null);
            }
            return(img);
        }
コード例 #2
0
        private Image CaptureWindow(IntPtr handle, int x, int y, int width, int height)
        {
            Image  img      = null;
            IntPtr srcDC    = IntPtr.Zero;
            IntPtr memoryDC = IntPtr.Zero;
            IntPtr bitmap   = IntPtr.Zero;

            try
            {
                // 해당 Handle의 크기 취득
                if (width == 0 || height == 0)
                {
                    var windowRect = new WindowNative.Rect();
                    var clientRect = new WindowNative.Rect();
                    WindowNative.GetWindowRect(handle, ref windowRect);
                    WindowNative.GetClientRect(handle, ref clientRect);
                    if (x == 0)
                    {
                        x = windowRect.Left;
                    }
                    if (y == 0)
                    {
                        y = windowRect.Top;
                    }
                    if (width == 0)
                    {
                        width = clientRect.Width;
                    }
                    if (height == 0)
                    {
                        height = windowRect.Height;
                    }

                    // Border 사이즈 제외
                    int diff = windowRect.Width - clientRect.Width;
                    if (diff > 0)
                    {
                        int borderSize = diff / 2;
                        x      += borderSize;
                        y      += borderSize;
                        height -= borderSize * 2;
                    }
                }

                // 해상도 스케일에 의한 크기 변경
                float scale = CalcScale();
                if (scale > 1)
                {
                    width  = (int)(width * scale);
                    height = (int)(height * scale);
                }

                handle   = WindowNative.GetDesktopWindow();
                srcDC    = WindowNative.GetWindowDC(handle);
                memoryDC = WindowNative.CreateCompatibleDC(srcDC);
                bitmap   = WindowNative.CreateCompatibleBitmap(srcDC, width, height);

                IntPtr oldBitmap = WindowNative.SelectObject(memoryDC, bitmap);
                WindowNative.BitBlt(memoryDC, 0, 0, width, height, srcDC, x, y, WindowNative.SRCCOPY | WindowNative.CAPTUREBLT);
                WindowNative.SelectObject(memoryDC, oldBitmap);

                img = Image.FromHbitmap(bitmap);
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            finally
            {
                WindowNative.DeleteObject(bitmap);
                WindowNative.DeleteDC(memoryDC);
                WindowNative.ReleaseDC(handle, srcDC);
            }

            return(img);
        }