From d0c5ed5e1ac82668de8851de713928c4334e7881 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Fri, 8 Nov 2024 09:03:47 +0300 Subject: [PATCH] Move base64 method to SnippingTool --- RhSolutions.AddIn/Tools/OcrTool.cs | 17 ++-------------- SnippingTool/{SnippingTool.cs => Snipper.cs} | 21 +++++++++++++++++--- SnippingTool/SnippingTool.Designer.cs | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) rename SnippingTool/{SnippingTool.cs => Snipper.cs} (85%) 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/SnippingTool.cs b/SnippingTool/Snipper.cs similarity index 85% rename from SnippingTool/SnippingTool.cs rename to SnippingTool/Snipper.cs index bd08076..0f5e9cc 100644 --- a/SnippingTool/SnippingTool.cs +++ b/SnippingTool/Snipper.cs @@ -1,6 +1,8 @@ +using System.Drawing.Imaging; + namespace SnippingTool; -public partial class SnippingTool : Form +public partial class Snipper : Form { public static Image? Snip() { @@ -8,12 +10,25 @@ public partial class SnippingTool : Form 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); + 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 SnippingTool(Image screenShot) + public Snipper(Image screenShot) { InitializeComponent(); BackgroundImage = screenShot; 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.