protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_HOTKEY) { ScreenshotTask info = GetParamteresFromUI(true); _worker = new Thread(() => Screenshot.CaptureWindow(ref info)) { IsBackground = true }; _worker.SetApartmentState(ApartmentState.STA); _worker.Start(); } if (m.Msg == WM_DWMCOMPOSITIONCHANGED) { WindowsApi.DwmIsCompositionEnabled(ref _dwmComposited); if (_dwmComposited) { ssButton.Location = new Point(ssButton.Location.X, 310); var margin = new WindowsMargins(0, 0, 0, 35); WindowsApi.DwmExtendFrameIntoClientArea(Handle, ref margin); } else { ssButton.Location = new Point(ssButton.Location.X, 305); } } }
private static bool AeroEnabled() { bool aeroEnabled; WindowsApi.DwmIsCompositionEnabled(out aeroEnabled); return(aeroEnabled); }
private static bool GlassAvailable() { if (Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor > 1) { return(false); } bool aeroEnabled; WindowsApi.DwmIsCompositionEnabled(out aeroEnabled); return(aeroEnabled); }
private static bool GlassAvailable() { if ((Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor > 1) || Environment.OSVersion.Version.Major >= 10 || Environment.OSVersion.Version.Major < 6) { return(false); } bool aeroEnabled = true; try { WindowsApi.DwmIsCompositionEnabled(out aeroEnabled); } catch (Exception) { //This is fallback intended for beta versions of Windows Vista } return(aeroEnabled); }
public MainForm() { DoubleBuffered = true; Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); InitializeComponent(); if (WindowsApi.DwmIsCompositionEnabled(ref _dwmComposited) == 0) { if (_dwmComposited) { ssButton.Location = new Point(ssButton.Location.X, 310); var margin = new WindowsMargins(0, 0, 0, 35); WindowsApi.DwmExtendFrameIntoClientArea(Handle, ref margin); } } _windowId = GetHashCode(); WindowsApi.RegisterHotKey(Handle, _windowId, MOD_ALT, (int)Keys.PrintScreen); object value; _registryKey = Registry.CurrentUser.CreateSubKey(@"Software\AeroShot"); if ((value = _registryKey.GetValue("LastPath")) != null && value.GetType() == (typeof(string))) { if (((string)value).Substring(0, 1) == "*") { folderTextBox.Text = ((string)value).Substring(1); clipboardButton.Checked = true; } else { folderTextBox.Text = (string)value; diskButton.Checked = true; } } else { folderTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } if ((value = _registryKey.GetValue("WindowSize")) != null && value.GetType() == (typeof(long))) { var b = new byte[8]; for (int i = 0; i < 8; i++) { b[i] = (byte)(((long)value >> (i * 8)) & 0xff); } resizeCheckbox.Checked = (b[0] & 1) == 1; windowWidth.Value = b[1] << 16 | b[2] << 8 | b[3]; windowHeight.Value = b[4] << 16 | b[5] << 8 | b[6]; } if ((value = _registryKey.GetValue("Opaque")) != null && value.GetType() == (typeof(long))) { var b = new byte[8]; for (int i = 0; i < 8; i++) { b[i] = (byte)(((long)value >> (i * 8)) & 0xff); } opaqueCheckbox.Checked = (b[0] & 1) == 1; if ((b[0] & 2) == 2) { opaqueType.SelectedIndex = 0; } if ((b[0] & 4) == 4) { opaqueType.SelectedIndex = 1; } checkerValue.Value = b[1] + 2; var hex = new StringBuilder(6); hex.AppendFormat("{0:X2}", b[2]); hex.AppendFormat("{0:X2}", b[3]); hex.AppendFormat("{0:X2}", b[4]); colourHexBox.Text = hex.ToString(); } else { opaqueType.SelectedIndex = 0; } if ((value = _registryKey.GetValue("CapturePointer")) != null && value.GetType() == (typeof(int))) { mouseCheckbox.Checked = ((int)value & 1) == 1; } groupBox1.Enabled = resizeCheckbox.Checked; groupBox2.Enabled = opaqueCheckbox.Checked; groupBox3.Enabled = mouseCheckbox.Checked; _ssButtonImage = Resources.capture; }
private static unsafe Bitmap[] CaptureCompositeScreenshot(ref ScreenshotTask data) { // Generate a rectangle with the size of all monitors combined Rectangle totalSize = Rectangle.Empty; foreach (Screen s in Screen.AllScreens) { totalSize = Rectangle.Union(totalSize, s.Bounds); } var rct = new WindowsRect(); if (WindowsApi.DwmGetWindowAttribute(data.WindowHandle, DwmWindowAttribute.ExtendedFrameBounds, ref rct, sizeof(WindowsRect)) != 0) { // DwmGetWindowAttribute() failed, usually means Aero is disabled so we fall back to GetWindowRect() WindowsApi.GetWindowRect(data.WindowHandle, ref rct); } else { // DwmGetWindowAttribute() succeeded } // Get DPI of the window (this only works properly on Win 10 1607+) but it is not really needed until Win 11 anyway int DPI = 96; try { DPI = WindowsApi.GetDpiForWindow(data.WindowHandle); } catch { } // Adjust margin for DPI double scalingFactor = DPI / 96; int backdropOffset = Convert.ToInt32(100 * scalingFactor); // Add a 100px margin for window shadows. Excess transparency is trimmed out later rct.Left -= backdropOffset; rct.Right += backdropOffset; rct.Top -= backdropOffset; rct.Bottom += backdropOffset; // These next 4 checks handle if the window is outside of the visible screen if (rct.Left < totalSize.Left) { rct.Left = totalSize.Left; } if (rct.Right > totalSize.Right) { rct.Right = totalSize.Right; } if (rct.Top < totalSize.Top) { rct.Top = totalSize.Top; } if (rct.Bottom > totalSize.Bottom) { rct.Bottom = totalSize.Bottom; } // Spawning backdrop // Handling as much as possible in the constructor makes this easier to render, which makes capture less likely to fail on underpowered PCs Color tmpColor = Color.White; var backdrop = new Form { BackColor = tmpColor, FormBorderStyle = FormBorderStyle.None, ShowInTaskbar = false, //Opacity = 0, Size = new Size(rct.Right - rct.Left, rct.Bottom - rct.Top), StartPosition = FormStartPosition.Manual, Location = new Point(rct.Left, rct.Top) }; WindowsApi.ShowWindow(backdrop.Handle, 4); if (!WindowsApi.SetWindowPos(backdrop.Handle, data.WindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_NOACTIVATE)) { // We are unable to put backdrop directly behind the window, so we will put it into the foreground and then put the original window on top of it // This likely happens because the program we're trying to capture is running as administrator WindowsApi.SetWindowPos(backdrop.Handle, IntPtr.Zero, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_NOACTIVATE); WindowsApi.SetForegroundWindow(data.WindowHandle).ToString(); } RefreshBackdrop(); // Capture screenshot with white background Bitmap whiteShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); backdrop.BackColor = Color.Black; RefreshBackdrop(); // Capture screenshot with black background Bitmap blackShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); Bitmap transparentImage; Bitmap transparentInactiveImage = null; Bitmap transparentWhiteImage = null; Bitmap transparentWhiteInactiveImage = null; Bitmap transparentMaskImage = null; Bitmap transparentTransparentImage = null; Bitmap transparentTransparentInactiveImage = null; transparentImage = DifferentiateAlpha(whiteShot, blackShot, false); if (data.SaveActiveLight) { transparentWhiteImage = DifferentiateAlpha(whiteShot, blackShot, true); } whiteShot.Dispose(); blackShot.Dispose(); //Capture black mask screenshot if (data.SaveMask) { int minAlpha = 0; bool isCompositing = false; bool ShadowToggled = false; bool ColorToggled = false; try { WindowsApi.DwmIsCompositionEnabled(out isCompositing); } catch (Exception) { //OS doesn't have a supported version of DWM } //We can't disable shadows on Vista without disabling DWM, which would cause the mask to be inaccurate UInt32 ColorizationColor = 0; bool fOpaque = true; if (isCompositing && (VersionHelpers.IsWindowsVista() || VersionHelpers.IsWindows11())) { minAlpha = 254; WindowsApi.DwmGetColorizationColor(out ColorizationColor, out fOpaque); if (fOpaque == false) { WindowsApi.DwmpSetColorization(ColorizationColor, true, 0xFF); ColorToggled = true; } } else if (ShadowEnabled()) { WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, false, 0); ShadowToggled = true; } backdrop.BackColor = Color.White; RefreshBackdrop(); Bitmap whiteMaskShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); backdrop.BackColor = Color.Black; RefreshBackdrop(); Bitmap blackMaskShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); transparentMaskImage = CreateMask(DifferentiateAlpha(whiteMaskShot, blackMaskShot, false), minAlpha); if (ShadowToggled) { WindowsApi.SystemParametersInfo(SPI_SETDROPSHADOW, 0, true, 0); } if (ColorToggled) { WindowsApi.DwmpSetColorization(ColorizationColor, fOpaque, 0xFF); } whiteMaskShot.Dispose(); blackMaskShot.Dispose(); } //Capture active fully transparent if (data.SaveActiveTransparent) { try { //win 7 WindowsApi.DWM_COLORIZATION_PARAMS parameters, originalParameters = new WindowsApi.DWM_COLORIZATION_PARAMS(); //win vista UInt32 ColorizationColor = 0; bool fOpaque = true; if (VersionHelpers.IsWindowsVista()) { WindowsApi.DwmGetColorizationColor(out ColorizationColor, out fOpaque); if (fOpaque == false) { WindowsApi.DwmpSetColorization(0xFFFFFF, false, 0xFF); } } else { WindowsApi.DwmGetColorizationParameters(out parameters); WindowsApi.DwmGetColorizationParameters(out originalParameters); //Set custom fully transparent parameters parameters.clrAfterGlowBalance = 0; parameters.clrBlurBalance = 100; parameters.nIntensity = 0; // Call the DwmSetColorizationParameters to make the change take effect. WindowsApi.DwmSetColorizationParameters(ref parameters, false); } backdrop.BackColor = Color.White; RefreshBackdrop(); Bitmap whiteTransparentShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); backdrop.BackColor = Color.Black; RefreshBackdrop(); Bitmap blackTransparentShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); transparentTransparentImage = DifferentiateAlpha(whiteTransparentShot, blackTransparentShot, false); whiteTransparentShot.Dispose(); blackTransparentShot.Dispose(); if (VersionHelpers.IsWindowsVista()) { WindowsApi.DwmpSetColorization(ColorizationColor, fOpaque, 0xFF); } else { WindowsApi.DwmSetColorizationParameters(ref originalParameters, false); } } catch (Exception) { transparentTransparentImage = new Bitmap(transparentImage); } } //Show form to steal focus var emptyForm = new Form { FormBorderStyle = FormBorderStyle.None, ShowInTaskbar = false, Opacity = 0, }; WindowsApi.ShowWindow(emptyForm.Handle, 5); WindowsApi.SetWindowPos(emptyForm.Handle, data.WindowHandle, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, 0); WindowsApi.SetForegroundWindow(emptyForm.Handle); // Capture inactive screenshots if (data.SaveInactiveDark || data.SaveInactiveLight) { backdrop.BackColor = Color.White; RefreshBackdrop(); // Capture inactive screenshot with white background Bitmap whiteInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); backdrop.BackColor = Color.Black; RefreshBackdrop(); // Capture inactive screenshot with black background Bitmap blackInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); if (data.SaveInactiveDark) { transparentInactiveImage = DifferentiateAlpha(whiteInactiveShot, blackInactiveShot, false); } if (data.SaveInactiveLight) { transparentWhiteInactiveImage = DifferentiateAlpha(whiteInactiveShot, blackInactiveShot, true); } whiteInactiveShot.Dispose(); blackInactiveShot.Dispose(); } //Capture inactive fully transparent if (data.SaveInactiveTransparent) { try { //Get original colorization parameters WindowsApi.DWM_COLORIZATION_PARAMS parameters, originalParameters; WindowsApi.DwmGetColorizationParameters(out parameters); WindowsApi.DwmGetColorizationParameters(out originalParameters); //Set custom fully transparent parameters parameters.clrAfterGlowBalance = 0; parameters.clrBlurBalance = 100; parameters.nIntensity = 0; // Call the DwmSetColorizationParameters to make the change take effect. WindowsApi.DwmSetColorizationParameters(ref parameters, false); backdrop.BackColor = Color.White; RefreshBackdrop(); Bitmap whiteTransparentInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); backdrop.BackColor = Color.Black; RefreshBackdrop(); Bitmap blackTransparentInactiveShot = CaptureScreenRegion(new Rectangle(rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top)); transparentTransparentInactiveImage = DifferentiateAlpha(whiteTransparentInactiveShot, blackTransparentInactiveShot, false); whiteTransparentInactiveShot.Dispose(); blackTransparentInactiveShot.Dispose(); WindowsApi.DwmSetColorizationParameters(ref originalParameters, false); } catch (Exception) { transparentTransparentInactiveImage = new Bitmap(transparentInactiveImage); } } backdrop.Dispose(); emptyForm.Dispose(); if (data.CaptureMouse) { DrawCursorToBitmap(transparentImage, new Point(rct.Left, rct.Top)); } Bitmap[] final = CropEmptyEdges(new[] { transparentImage, transparentInactiveImage, transparentWhiteImage, transparentWhiteInactiveImage, transparentMaskImage, transparentTransparentImage, transparentTransparentInactiveImage }, Color.FromArgb(0, 0, 0, 0), ref data); // Returns a bitmap with transparency, calculated by differentiating the white and black screenshots return(final); }