コード例 #1
0
        public static Bitmap CaptureRegion(Rect region)
        {
            Bitmap result;

            // ReSharper disable once IdentifierTypo
            var desktophWnd = NativeScreenShotMethods.GetDesktopWindow();
            var desktopDc   = NativeScreenShotMethods.GetWindowDC(desktophWnd);
            var memoryDc    = NativeScreenShotMethods.CreateCompatibleDC(desktopDc);
            var bitmap      =
                NativeScreenShotMethods.CreateCompatibleBitmap(desktopDc, (int)region.Width, (int)region.Height);
            var oldBitmap = NativeScreenShotMethods.SelectObject(memoryDc, bitmap);

            var success = NativeScreenShotMethods.BitBlt(memoryDc, 0, 0, (int)region.Width, (int)region.Height,
                                                         desktopDc, (int)region.Left, (int)region.Top,
                                                         NativeScreenShotMethods.RasterOperations.SRCCOPY | NativeScreenShotMethods.RasterOperations.CAPTUREBLT);

            try
            {
                if (!success)
                {
                    throw new Win32Exception();
                }

                result = Image.FromHbitmap(bitmap);
            }
            finally
            {
                NativeScreenShotMethods.SelectObject(memoryDc, oldBitmap);
                NativeScreenShotMethods.DeleteObject(bitmap);
                NativeScreenShotMethods.DeleteDC(memoryDc);
                NativeScreenShotMethods.ReleaseDC(desktophWnd, desktopDc);
            }

            return(result);
        }
コード例 #2
0
        //https://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap/1118557#1118557
        public static BitmapSource BitmapSourceFromSystemDrawingBitmap(Bitmap source)
        {
            var          ip = source.GetHbitmap();
            BitmapSource bs;

            try
            {
                bs = Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
                                                           BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                NativeScreenShotMethods.DeleteObject(ip);
            }

            return(bs);
        }
コード例 #3
0
        public static Bitmap CaptureWindow(IntPtr hWnd)
        {
            NativeScreenShotMethods.RECT region;

            if (Environment.OSVersion.Version.Major < 6)
            {
                NativeScreenShotMethods.GetWindowRect(hWnd, out region);
            }
            else
            {
                if (NativeScreenShotMethods.DwmGetWindowAttribute(hWnd,
                                                                  NativeScreenShotMethods.DWMWA_EXTENDED_FRAME_BOUNDS, out region,
                                                                  Marshal.SizeOf(typeof(NativeScreenShotMethods.RECT))) !=
                    0)
                {
                    NativeScreenShotMethods.GetWindowRect(hWnd, out region);
                }
            }

            return(CaptureRegion(new Rect(new Point(region.left, region.top),
                                          new Size(region.right - region.left, region.bottom - region.top))));
        }
コード例 #4
0
 public static Bitmap CaptureActiveWindow()
 {
     return(CaptureWindow(NativeScreenShotMethods.GetForegroundWindow()));
 }