From d0c5ed5e1ac82668de8851de713928c4334e7881 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Fri, 8 Nov 2024 09:03:47 +0300 Subject: Move base64 method to SnippingTool --- RhSolutions.AddIn/Tools/OcrTool.cs | 17 +----- SnippingTool/Snipper.cs | 101 ++++++++++++++++++++++++++++++++++ SnippingTool/SnippingTool.Designer.cs | 2 +- SnippingTool/SnippingTool.cs | 86 ----------------------------- 4 files changed, 104 insertions(+), 102 deletions(-) create mode 100644 SnippingTool/Snipper.cs delete mode 100644 SnippingTool/SnippingTool.cs diff --git a/RhSolutions.AddIn/Tools/OcrTool.cs b/RhSolutions.AddIn/Tools/OcrTool.cs index a30b27c..f407db8 100644 --- a/RhSolutions.AddIn/Tools/OcrTool.cs +++ b/RhSolutions.AddIn/Tools/OcrTool.cs @@ -1,10 +1,5 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif - -using System.Drawing.Imaging; -using System.IO; using System.Threading.Tasks; +using SnippingTool; namespace RhSolutions.Tools; @@ -24,15 +19,7 @@ internal class OcrTool : Tool await Task.Delay(100); }).Wait(); - var shot = SnippingTool.SnippingTool.Snip(); - if (shot != null) - { - using MemoryStream ms = new(); - shot.Save(ms, ImageFormat.Png); - byte[] imageBytes = ms.ToArray(); - string base64 = Convert.ToBase64String(imageBytes); - } - + string shot = Snipper.SnipBase64(); Application.Visible = true; } } \ No newline at end of file diff --git a/SnippingTool/Snipper.cs b/SnippingTool/Snipper.cs new file mode 100644 index 0000000..0f5e9cc --- /dev/null +++ b/SnippingTool/Snipper.cs @@ -0,0 +1,101 @@ +using System.Drawing.Imaging; + +namespace SnippingTool; + +public partial class Snipper : 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 Snipper(bmp); + + return snipper.ShowDialog() == DialogResult.OK ? snipper.Image : null; + } + + public static string? SnipBase64() + { + var shot = Snip(); + if (shot != null) + { + using MemoryStream ms = new(); + shot.Save(ms, ImageFormat.Png); + byte[] imageBytes = ms.ToArray(); + return Convert.ToBase64String(imageBytes); + } + return null; + } + + public Snipper(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.Designer.cs b/SnippingTool/SnippingTool.Designer.cs index 322a511..a170216 100644 --- a/SnippingTool/SnippingTool.Designer.cs +++ b/SnippingTool/SnippingTool.Designer.cs @@ -1,6 +1,6 @@ namespace SnippingTool; -partial class SnippingTool +partial class Snipper { /// /// Required designer variable. diff --git a/SnippingTool/SnippingTool.cs b/SnippingTool/SnippingTool.cs deleted file mode 100644 index bd08076..0000000 --- a/SnippingTool/SnippingTool.cs +++ /dev/null @@ -1,86 +0,0 @@ -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 -- cgit v1.2.3