using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace FCC_Uber_MDX_Configurator { public partial class AddImageForm : Form { public string IMAGE_PATH; public Bitmap IMAGE_BITMAP; public AddImageForm() { InitializeComponent(); this.Shown += new EventHandler(AddImageForm_Shown); } void AddImageForm_Shown(object sender, EventArgs e) { ShowLoadImageDialogue(); } public AddImageForm(Bitmap input_Bitmap, string input_path) { IMAGE_PATH = input_path; IMAGE_BITMAP = input_Bitmap; InitializeComponent(); RefreshPictureBox(); } private void RefreshPictureBox() { if (IMAGE_BITMAP.Size.Width > PICT_PREVIEW.Size.Width || IMAGE_BITMAP.Size.Height > PICT_PREVIEW.Size.Height) { PICT_PREVIEW.BackgroundImageLayout = ImageLayout.Zoom; } else { PICT_PREVIEW.BackgroundImageLayout = ImageLayout.Center; } PICT_PREVIEW.BackgroundImage = IMAGE_BITMAP; } private void ShowLoadImageDialogue() { if (DIALOGUE_OPEN.ShowDialog() == DialogResult.OK) { this.DialogResult = DialogResult.None; if (!File.Exists(DIALOGUE_OPEN.FileName)) { (new ErrorForm("Invalid Image", "Image does not exist")).ShowDialog(); return; } List supportedFileExtensions = new List(new string[] { "png", "bmp", "jpg", "gif" }); string[] currentFileExtensionArray = DIALOGUE_OPEN.FileName.Split(new char[] { '.' }); if (currentFileExtensionArray.Length != 2) { (new ErrorForm("Invalid File Name", "File cannot contain multiple extensions\r\n(Only 1 \".\" allowed)")).ShowDialog(); return; } string currentFileExtension = currentFileExtensionArray[1]; if (!supportedFileExtensions.Contains(currentFileExtension)) { (new ErrorForm("Invalid Image", "Image is not of a supported file type")).ShowDialog(); return; } IMAGE_PATH = DIALOGUE_OPEN.FileName; IMAGE_BITMAP = (Bitmap)Image.FromFile(IMAGE_PATH); RefreshPictureBox(); return; } this.DialogResult = DialogResult.None; } private void BTN_LOAD_Click(object sender, EventArgs e) { ShowLoadImageDialogue(); } private void BTN_SAVE_Click(object sender, EventArgs e) { if (IMAGE_PATH == null || IMAGE_PATH == "") { (new ErrorForm("Invalid Image", "No image or an invalid image has been loaded.")).ShowDialog(); return; } if (!System.IO.File.Exists(IMAGE_PATH)) { (new ErrorForm("File Not Found", "This image is no longer in the location it was.")).ShowDialog(); return; } this.DialogResult = DialogResult.OK; } private void BTN_CANCEL_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } } }