From 93dc7fd66ed45207f22391035a23ffb6617451eb Mon Sep 17 00:00:00 2001
From: Ryan <27cobalt60@gmail.com>
Date: Mon, 17 Feb 2020 22:17:28 +0000
Subject: [PATCH] Fixed certain user input cases causing a crash
Can now change width variable with no image loaded. Width only changes when image is present.
Can now click convert button with no image loaded.
Can now click convert button while there are no colours in the palette.
Conversion now only happens if width is greater than 0, an image has been loaded, and there is at least one colour in the palette.
Can now press cancel in the load dialogue, without a crash.
---
DMCConverter/Form1.cs | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)
diff --git a/DMCConverter/Form1.cs b/DMCConverter/Form1.cs
index f57eafd..671be3e 100644
--- a/DMCConverter/Form1.cs
+++ b/DMCConverter/Form1.cs
@@ -14,6 +14,7 @@ namespace DMCConverter
{
public partial class Form1 : Form
{
+ public bool imageLoaded = false;
public int maxSize = 100;
public int tickedCount = 0;
public Image toConvert;
@@ -23,6 +24,7 @@ public partial class Form1 : Form
public string[,] dmcDataStore;
public Color[,] rgbArray;
+ private Image image;
public Form1()
{
@@ -59,7 +61,17 @@ public void LoadImageButon_Click(object sender, EventArgs e)
string targetFile = Path.ChangeExtension(sourceFile, "png");
//load image png and assign it to an Image variable
- Image image = Image.FromFile(sourceFile);
+
+ //this is here to catch the event a user opens the load dialogue and presses the cancel button, without loading an image.
+ try
+ {
+ image = Image.FromFile(sourceFile);
+ }
+ catch (Exception)
+ {
+
+ return;
+ }
Console.WriteLine("loading " + openFileDialog1.FileName.ToString());
//load image to image display box
@@ -81,6 +93,7 @@ public void LoadImageButon_Click(object sender, EventArgs e)
///
public void dmcPaletteBox_SelectedIndexChanged(object sender, EventArgs e)
{
+
//gets how many DMC values the user has selected
tickedCount = dmcPaletteBox.CheckedItems.Count;
@@ -99,6 +112,17 @@ public void dmcPaletteBox_SelectedIndexChanged(object sender, EventArgs e)
///
public void ConvertButton_Click(object sender, EventArgs e)
{
+ //checking ticked count makes sure we dont try and run the conversion without a palette
+ if (tickedCount == 0)
+ {
+ return;
+ }
+ //checking if image is null makes sure we dont run the conversion without an image present
+ if (UserImageBox.Image == null)
+ {
+ return;
+ }
+
//if user converts a second image, we have to clear the data from the first conversion
if (DMCDataGrid.RowCount > 0 )
{
@@ -133,6 +157,11 @@ public void EnableDoubleBuffering()
public void WidthValue_ValueChanged(object sender, EventArgs e)
{
+ if (UserImageBox.Image == null)
+ {
+ return;
+ }
+
resized = ConvertImg.resizeImage(toConvert,
toConvert.Width,
toConvert.Height,