I'm trying to create a from to visualize and edit images, simple stuff like:
- Drawing rectangles
- Writing stuff
- Cropping rectangles
But I'm encountering a lot of difficulties, for instance: If I try to load a file that doesn't exist or that can't be open (it's not a image), I'd like to show a MessageBox and then close the form. But the form doesn't close immediately (which may cause errors, since I may try to access properties of a file that wasn't open). Can you please tell me why? (check the Abort() Methods) The source code is in the end of the post.
I create my form inside with the following event:
private void button2_Click(object sender, EventArgs e)
        {
            Forms.AreaSelector areaSelector = new Forms.AreaSelector(LabelInput);
            areaSelector.ShowDialog();
        }
I'd like to show my form as a dialog so the user can't go back to the 'main window' without fishing the modifications to the image, that's why I'm using .ShowDialog(), instead of Show() I tried calling Close() and even Dispose() in my "Abort" method, but the form continues to load (by continues to load I mean that UpdateWindowSize() and UpdatePictureBox() are called regarless of what I do in Abort()).
And here's the actual form code.
Source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PeeAlyser.Forms
{
    public partial class AreaSelector : Form
    {
        #region Variables
        Bitmap originalImage, modifiedImage;
        string fileName;
        #endregion
        #region Constructors
        public AreaSelector(string fileName)
        {
            InitializeComponent();
            this.fileName = fileName;
        }
        #endregion
        private void AreaSelector_Load(object sender, EventArgs e)
        {
            TryToLoadImage();
            UpdateWindowSize();
            UpdatePictureBox();
        }
        #region Private Methods
        private void Abort()
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.BeginInvoke(new MethodInvoker(this.Close));
            //this.Close();
            //this.Dispose();
            // GODAMNIT!
        }
        private void TryToLoadImage()
        {
            if (!System.IO.File.Exists(fileName))
            {
                MessageBox.Show("File not found.");
                Abort();
            }
            try { originalImage = (Bitmap)Bitmap.FromFile(fileName); }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + Environment.NewLine +
                                error.ToString());
                Abort();
            }
            this.modifiedImage = new Bitmap(originalImage);
        }
        private void UpdateWindowSize()
        {
            int widthDifference = this.Width - pictureBox1.Width;
            int heightDifference = this.Height - pictureBox1.Height;
            Size windowSize = originalImage.Size;
            windowSize.Width += widthDifference;
            windowSize.Height += heightDifference;
            this.Size = this.MinimumSize = this.MaximumSize = windowSize;
            this.pictureBox1.Size = originalImage.Size;
            this.AdjustFormScrollbars(true);
        }
        private void UpdatePictureBox()
        {
            this.pictureBox1.Image = modifiedImage;
            this.Refresh();
        }
        #endregion
    }
}
Edit: I received a lot of suggestions to work around it. But Hans's answer not only corrected a design flaw in my correct (that also solved this issue) but also explained why this kind of issue happens (check his link). So I choose his answer. Feel free to close this quesiton, mods. And thanks for the help guys!
 
     
     
     
    