From 124dea6fc2c32f8ac96fb806a02bf288e9e15dd4 Mon Sep 17 00:00:00 2001 From: pointfeev Date: Fri, 25 Mar 2022 22:38:52 -0400 Subject: [PATCH] window screenshot shenanigans --- CreamInstaller/Components/CustomForm.cs | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/CreamInstaller/Components/CustomForm.cs b/CreamInstaller/Components/CustomForm.cs index 57ddb43..287cfec 100644 --- a/CreamInstaller/Components/CustomForm.cs +++ b/CreamInstaller/Components/CustomForm.cs @@ -1,4 +1,7 @@ -using System.Windows.Forms; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using System.Windows.Forms; namespace CreamInstaller.Components; @@ -6,7 +9,12 @@ internal class CustomForm : Form { internal CustomForm() : base() => Icon = Properties.Resources.Icon; - internal CustomForm(IWin32Window owner) : this() => Owner = (owner as Form) ?? ActiveForm; + internal CustomForm(IWin32Window owner) : this() + { + Owner = (owner as Form) ?? ActiveForm; + KeyPreview = true; + KeyPress += OnKeyPress; + } protected override CreateParams CreateParams // Double buffering for all controls { @@ -24,4 +32,20 @@ internal class CustomForm : Form int Y = fromForm.Location.Y + fromForm.Size.Height / 2 - Size.Height / 2; Location = new(X, Y); } + + private void OnKeyPress(object s, KeyPressEventArgs e) + { + if (e.KeyChar != 'S') return; // Shift + S + UpdateBounds(); + Rectangle bounds = Bounds; + using Bitmap bitmap = new(Size.Width - 14, Size.Height - 7); + using Graphics graphics = Graphics.FromImage(bitmap); + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + ImageCodecInfo jpeg = ImageCodecInfo.GetImageEncoders()[1]; + using EncoderParameters encoding = new(1); + encoding.Param[0] = new(Encoder.Quality, 100L); + graphics.CopyFromScreen(new(bounds.Left + 7, bounds.Top), Point.Empty, new(Size.Width - 14, Size.Height - 7)); + Clipboard.SetImage(bitmap); + e.Handled = true; + } }