diff options
author | Serghei Cebotari <serghei@cebotari.ru> | 2024-11-06 23:43:00 +0300 |
---|---|---|
committer | Serghei Cebotari <serghei@cebotari.ru> | 2024-11-06 23:43:00 +0300 |
commit | dbaa8b111a5d869159db7e58b14c026806265874 (patch) | |
tree | 191b74ec9163cfdb693b1cc0e87081065a81bfa0 /SnippingTool | |
parent | a065c4c6990e959742faa1bf8bb7ef960146978b (diff) |
Add snipping tool
Diffstat (limited to 'SnippingTool')
-rw-r--r-- | SnippingTool/SnippingTool.Designer.cs | 38 | ||||
-rw-r--r-- | SnippingTool/SnippingTool.cs | 86 | ||||
-rw-r--r-- | SnippingTool/SnippingTool.csproj | 12 |
3 files changed, 136 insertions, 0 deletions
diff --git a/SnippingTool/SnippingTool.Designer.cs b/SnippingTool/SnippingTool.Designer.cs new file mode 100644 index 0000000..322a511 --- /dev/null +++ b/SnippingTool/SnippingTool.Designer.cs @@ -0,0 +1,38 @@ +namespace SnippingTool; + +partial class SnippingTool +{ + /// <summary> + /// Required designer variable. + /// </summary> + private System.ComponentModel.IContainer components = null; + + /// <summary> + /// Clean up any resources being used. + /// </summary> + /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// <summary> + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// </summary> + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "Form1"; + } + + #endregion +} diff --git a/SnippingTool/SnippingTool.cs b/SnippingTool/SnippingTool.cs new file mode 100644 index 0000000..bd08076 --- /dev/null +++ b/SnippingTool/SnippingTool.cs @@ -0,0 +1,86 @@ +namespace SnippingTool; + +public partial class SnippingTool : Form +{ + public static Image? Snip() + { + var rc = Screen.PrimaryScreen.Bounds; + using Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); + using Graphics gr = Graphics.FromImage(bmp); + gr.CopyFromScreen(0, 0, 0, 0, bmp.Size); + using var snipper = new SnippingTool(bmp); + + return snipper.ShowDialog() == DialogResult.OK ? snipper.Image : null; + } + + public SnippingTool(Image screenShot) + { + InitializeComponent(); + BackgroundImage = screenShot; + ShowInTaskbar = false; + FormBorderStyle = FormBorderStyle.None; + WindowState = FormWindowState.Maximized; + DoubleBuffered = true; + } + public Image? Image { get; set; } + + private Rectangle rcSelect = new Rectangle(); + private Point pntStart; + + protected override void OnMouseDown(MouseEventArgs e) + { + if (e.Button != MouseButtons.Left) + { + return; + } + pntStart = e.Location; + rcSelect = new Rectangle(e.Location, new Size(0, 0)); + Invalidate(); + } + protected override void OnMouseMove(MouseEventArgs e) + { + if (e.Button != MouseButtons.Left) + { + return; + } + int x1 = Math.Min(e.X, pntStart.X); + int y1 = Math.Min(e.Y, pntStart.Y); + int x2 = Math.Max(e.X, pntStart.X); + int y2 = Math.Max(e.Y, pntStart.Y); + rcSelect = new Rectangle(x1, y1, x2 - x1, y2 - y1); + Invalidate(); + } + protected override void OnMouseUp(MouseEventArgs e) + { + if (rcSelect.Width <= 0 || rcSelect.Height <= 0) + { + return; + } + Image = new Bitmap(rcSelect.Width, rcSelect.Height); + using Graphics gr = Graphics.FromImage(Image); + gr.DrawImage(BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height), + rcSelect, GraphicsUnit.Pixel); + DialogResult = DialogResult.OK; + } + protected override void OnPaint(PaintEventArgs e) + { + using Brush br = new SolidBrush(Color.FromArgb(120, Color.White)); + int x1 = rcSelect.X; int x2 = rcSelect.X + rcSelect.Width; + int y1 = rcSelect.Y; int y2 = rcSelect.Y + rcSelect.Height; + e.Graphics.FillRectangle(br, new Rectangle(0, 0, x1, Height)); + e.Graphics.FillRectangle(br, new Rectangle(x2, 0, Width - x2, Height)); + e.Graphics.FillRectangle(br, new Rectangle(x1, 0, x2 - x1, y1)); + e.Graphics.FillRectangle(br, new Rectangle(x1, y2, x2 - x1, Height - y2)); + + using Pen pen = new Pen(Color.Red, 3); + e.Graphics.DrawRectangle(pen, rcSelect); + } + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + { + if (keyData == Keys.Escape) + { + DialogResult = DialogResult.Cancel; + } + return base.ProcessCmdKey(ref msg, keyData); + } +}
\ No newline at end of file diff --git a/SnippingTool/SnippingTool.csproj b/SnippingTool/SnippingTool.csproj new file mode 100644 index 0000000..a16e30c --- /dev/null +++ b/SnippingTool/SnippingTool.csproj @@ -0,0 +1,12 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Library</OutputType> + <LangVersion>10</LangVersion> + <TargetFrameworks>net472;net6.0-windows</TargetFrameworks> + <Nullable>enable</Nullable> + <UseWindowsForms>true</UseWindowsForms> + <ImplicitUsings>enable</ImplicitUsings> + </PropertyGroup> + +</Project>
\ No newline at end of file |