From fdb8a8fef662b02a8dc1c66115f0fbf7339a81eb Mon Sep 17 00:00:00 2001 From: MrFlapstaart <111083167+MrFlapstaart@users.noreply.github.com> Date: Fri, 12 Aug 2022 14:22:03 +0200 Subject: [PATCH] - Fixed #2 image file filter is applied, exception handling on image file loading. - Removed redundant declarations in OCRResult, added a base class OCRDimBase --- MainForm.Designer.cs | 3 +- MainForm.cs | 18 +++++++-- OCRResult.cs | 93 ++++++++------------------------------------ 3 files changed, 32 insertions(+), 82 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index b6c59bd..3ea9201 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -161,7 +161,8 @@ private void InitializeComponent() // // imageOpenDialog // - this.imageOpenDialog.Filter = "All Image Files (*.png, *.tiff, *.jpg)|*.*"; + this.imageOpenDialog.Filter = "All Image Files (*.png, *.tiff, *.jpg, *.jpeg, *.bmp)|*.png;*.jpg;*.bmp;*.tiff;*." + + "jpeg"; // // colorSelect // diff --git a/MainForm.cs b/MainForm.cs index 71b0663..fd3f9df 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -55,11 +55,21 @@ private void button1_Click(object sender, EventArgs e) return; Logger.Start(); - Logger.AddLog("Opening file"); - Image testimage = Bitmap.FromFile(imageOpenDialog.FileName); - Bitmap bitmap = new Bitmap(testimage); + Logger.AddLog("Opening file"); + try + { + Image testimage = Bitmap.FromFile(imageOpenDialog.FileName); + Bitmap bitmap = new Bitmap(testimage); + ProcessImage(bitmap); + } + catch + { + Logger.AddLog($"Error opening image file"); + logBox.Text = Logger.Finish(); + MessageBox.Show("This is not a valid image file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } - ProcessImage(bitmap); + } private void ProcessImage(Bitmap bitmap) diff --git a/OCRResult.cs b/OCRResult.cs index 64caab8..c730969 100644 --- a/OCRResult.cs +++ b/OCRResult.cs @@ -7,9 +7,8 @@ namespace GameOCRTTS { - [XmlRoot(ElementName = "String")] - public class String - { + public class OCRDimBase + { [XmlAttribute(AttributeName = "ID")] public string Id { get; set; } @@ -25,6 +24,11 @@ public class String [XmlAttribute(AttributeName = "HEIGHT")] public string Height { get; set; } + } + + [XmlRoot(ElementName = "String")] + public class TextString : OCRDimBase + { [XmlAttribute(AttributeName = "WC")] public string WC { get; set; } @@ -33,104 +37,39 @@ public class String } [XmlRoot(ElementName = "TextLine")] - public class TextLine + public class TextLine : OCRDimBase { [XmlElement(ElementName = "String")] - public List String { get; set; } - - [XmlAttribute(AttributeName = "ID")] - public string Id { get; set; } - - [XmlAttribute(AttributeName = "HPOS")] - public string HPos { get; set; } - - [XmlAttribute(AttributeName = "VPOS")] - public string VPos { get; set; } - - [XmlAttribute(AttributeName = "WIDTH")] - public string Width { get; set; } - - [XmlAttribute(AttributeName = "HEIGHT")] - public string Height { get; set; } + public List TextString { get; set; } [XmlElement(ElementName = "SP")] public List SP { get; set; } } [XmlRoot(ElementName = "TextBlock")] - public class TextBlock + public class TextBlock : OCRDimBase { [XmlElement(ElementName = "TextLine")] - public List TextLine { get; set; } - - [XmlAttribute(AttributeName = "ID")] - public string Id { get; set; } - - [XmlAttribute(AttributeName = "HPOS")] - public string HPos { get; set; } - - [XmlAttribute(AttributeName = "VPOS")] - public string VPos { get; set; } - - [XmlAttribute(AttributeName = "WIDTH")] - public string Width { get; set; } - - [XmlAttribute(AttributeName = "HEIGHT")] - public string Height { get; set; } + public List TextLine { get; set; } } [XmlRoot(ElementName = "ComposedBlock")] - public class ComposedBlock + public class ComposedBlock : OCRDimBase { [XmlElement(ElementName = "TextBlock")] - public TextBlock TextBlock { get; set; } - - [XmlAttribute(AttributeName = "ID")] - public string Id { get; set; } - - [XmlAttribute(AttributeName = "HPOS")] - public string HPos { get; set; } - - [XmlAttribute(AttributeName = "VPOS")] - public string VPos { get; set; } - - [XmlAttribute(AttributeName = "WIDTH")] - public string Width { get; set; } - - [XmlAttribute(AttributeName = "HEIGHT")] - public string Height { get; set; } + public TextBlock TextBlock { get; set; } } [XmlRoot(ElementName = "SP")] - public class SP + public class SP : OCRDimBase { - [XmlAttribute(AttributeName = "WIDTH")] - public string Width { get; set; } - - [XmlAttribute(AttributeName = "VPOS")] - public string VPos { get; set; } - - [XmlAttribute(AttributeName = "HPOS")] - public string HPos { get; set; } } [XmlRoot(ElementName = "PrintSpace")] - public class PrintSpace + public class PrintSpace : OCRDimBase { [XmlElement(ElementName = "ComposedBlock")] - public List ComposedBlock { get; set; } - - [XmlAttribute(AttributeName = "HPOS")] - public string HPos { get; set; } - - [XmlAttribute(AttributeName = "VPOS")] - public string VPos { get; set; } - - [XmlAttribute(AttributeName = "WIDTH")] - public string Width { get; set; } - - [XmlAttribute(AttributeName = "HEIGHT")] - public string Height { get; set; } + public List ComposedBlock { get; set; } } [XmlRoot(ElementName = "Page")]