Skip to content

Commit

Permalink
Fixed certain user input cases causing a crash
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ryason committed Feb 17, 2020
1 parent 7de067d commit 93dc7fd
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion DMCConverter/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ public partial class Form1 : Form

public string[,] dmcDataStore;
public Color[,] rgbArray;
private Image image;

public Form1()
{
Expand Down Expand Up @@ -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
Expand All @@ -81,6 +93,7 @@ public void LoadImageButon_Click(object sender, EventArgs e)
/// <param name="e"></param>
public void dmcPaletteBox_SelectedIndexChanged(object sender, EventArgs e)
{

//gets how many DMC values the user has selected
tickedCount = dmcPaletteBox.CheckedItems.Count;

Expand All @@ -99,6 +112,17 @@ public void dmcPaletteBox_SelectedIndexChanged(object sender, EventArgs e)
/// <param name="e"></param>
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 )
{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 93dc7fd

Please sign in to comment.