public ImageCaptureWindow(System.Drawing.Rectangle rect, AreaSelection areaSelection)
        {
            InitializeComponent();

            MouseUp   += OnMouseUp;
            MouseDown += OnMouseDown;
            MouseMove += OnMouseMove;
            Loaded    += OnMainWindowLoaded;
            _scalor    = DpiUtilities.GetVirtualPixelScale(this);

            _areaSelection = areaSelection;
            var monitors = _areaSelection.GetMonitorsInformation();

            _screenShotImage = new ScreenshotImage();

            _screenScalor = DpiUtilities.GetScreenScalingFactor();
            _screenShotImage.SnapShot(rect, _screenScalor);

            WindowStartupLocation = WindowStartupLocation.Manual;

            SourceInitialized += (sender, e) =>
            {
                IntPtr hWnd = new WindowInteropHelper(this).Handle;
                NativeMethods.SetWindowPos(hWnd, (IntPtr)NativeMethods.SetWindowPosInsertAfter.HWND_TOP, rect.Left, rect.Top, rect.Width, rect.Height, 0);
            };

            var bmDesktopSource = ScreenCapture.GetBitmapSource(_screenShotImage.ScreenSnapshotImage);

            BackgroundImage.Fill = new ImageBrush(bmDesktopSource);
            // MagnifierBackgroundImage.Source = bmDesktopSource;
        }
Beispiel #2
0
        public MonitorInformation GetMonitorInformation(IntPtr hMonitor)
        {
            if (_monitorInfoCache.ContainsKey(hMonitor))
            {
                return(_monitorInfoCache[hMonitor]);
            }

            if (hMonitor == IntPtr.Zero)
            {
                // Get handle to the default monitor
                const int MONITOR_DEFAULTTOPRIMARY = 0x00000001;
                hMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, MONITOR_DEFAULTTOPRIMARY);
                if (_monitorInfoCache.ContainsKey(hMonitor))
                {
                    _monitorInfoCache[IntPtr.Zero] = _monitorInfoCache[hMonitor];
                    return(_monitorInfoCache[hMonitor]);
                }
            }

            var monitorInfo = NativeMethods.MONITORINFOEX.New();

            if (!NativeMethods.GetMonitorInfoEx(hMonitor, ref monitorInfo))
            {
                _monitorInfoCache[hMonitor] = null;
                return(null);
            }

            System.Diagnostics.Trace.WriteLine("Monitor:\"" + monitorInfo.deviceName + "\" Handle:" + hMonitor
                                               + " Left:" + monitorInfo.rcMonitor.left + " Top:" + monitorInfo.rcMonitor.top
                                               + " Right:" + monitorInfo.rcMonitor.right + " Bottom:" + monitorInfo.rcMonitor.bottom);

            UInt32 effectiveDPIx, effectiveDPIy;

            //GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_Effective_DPI, out effectiveDPIx, out effectiveDPIy);
            DpiUtilities.GetMonitorEffectiveDpi(hMonitor, out effectiveDPIx, out effectiveDPIy);
            System.Diagnostics.Trace.WriteLine("Effective DPI:" + effectiveDPIx + " " + effectiveDPIy);

            //UInt32 rawDPIx, rawDPIy;
            //GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_Raw_DPI, out rawDPIx, out rawDPIy);
            //System.Diagnostics.Trace.WriteLine("Raw DPI:" + rawDPIx + " " + rawDPIy);

            var monitorInformation = new MonitorInformation()
            {
                deviceName    = monitorInfo.deviceName,
                isPrimary     = (monitorInfo.dwFlags & NativeMethods.MONITORINFOF_PRIMARY) != 0,
                rcMonitor     = monitorInfo.rcMonitor,
                rcWork        = monitorInfo.rcWork,
                scalingFactor = DpiUtilities.GetScreenScalingFactor(monitorInfo.deviceName),
                dpiX          = effectiveDPIx,
                dpiY          = effectiveDPIy,
            };

            _monitorInfoCache[hMonitor] = monitorInformation;
            if (monitorInformation.isPrimary && !_monitorInfoCache.ContainsKey(IntPtr.Zero))
            {
                _monitorInfoCache[IntPtr.Zero] = monitorInformation;
            }
            return(monitorInformation);
        }