private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (_img != null) { var filter = comboBox1.SelectedItem.ToString(); switch (filter) { case "Negative": _filter = filter.ToLower(); var negative = new NegativeImage(_img); _newImg = negative.AddNegativeFilter(); ShowImage(_newImg); break; case "Grayscale": _filter = filter.ToLower(); var grayscale = new GrayscaleImage(_img); _newImg = grayscale.AddGrayscaleFilter(); ShowImage(_newImg); break; case "Blurred": _filter = filter.ToLower(); var blur = new BlurredImage(_img); _newImg = blur.AddBlurFilter(); ShowImage(_newImg); break; } } else { MessageBox.Show("No image has been selected, please select an image for continuing", "No Image", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static string Process(string str) { var message = ""; var valid = true; Bitmap img = null; string path = null; try { path = str; img = new Bitmap(path); } catch (FileNotFoundException) { return("Something was wrong with the input, Please try again"); } catch (IndexOutOfRangeException) { return("Something was wrong with the input, Please try again"); } catch (ArgumentException) { return("Something was wrong with the input, Please try again"); } if (valid && img != null && path != null) { if (!FileHandler.CheckPath(path)) { return("Something was wrong with the input, Please try again"); } // Check if size is over 400x400 = 160 000 else if (!FileHandler.CheckImageSize(img)) { return("The image you have entered is too big, Please enter a smaller one"); } else { // Create and save a negative image var negative = new NegativeImage(img); var negativeImage = negative.AddNegativeFilter(); negativeImage.Save(FileHandler.GetNewPath(path, "_negative")); // Create and save a grayscale image var grayscale = new GrayscaleImage(img); var grayscaleImage = grayscale.AddGrayscaleFilter(); grayscaleImage.Save(FileHandler.GetNewPath(path, "_grayscale")); // Create and save a blurred image var blurred = new BlurredImage(img); var blurredImage = blurred.AddBlurFilter(); blurredImage.Save(FileHandler.GetNewPath(path, "_blurred")); // If everything works, exit the application Environment.Exit(0); } } return(message); }
public void AddNegativeFilter_FilterAdded_ColorsNegative() { // Arrange const int width = 3; const int height = 3; var img = new Bitmap(width, height); var expectedImg = new Bitmap(width, height); for (var x = 0; x < img.Width; x++) { for (var y = 0; y < img.Height; y++) { const int alpha = 255; var val = 255; if (y % 2 == 0) { val = 0; } img.SetPixel(x, y, Color.FromArgb(alpha, val, val, val)); } } expectedImg.SetPixel(0, 0, Color.FromArgb(255, 255, 255, 255)); expectedImg.SetPixel(0, 1, Color.FromArgb(255, 0, 0, 0)); expectedImg.SetPixel(0, 2, Color.FromArgb(255, 255, 255, 255)); expectedImg.SetPixel(1, 0, Color.FromArgb(255, 255, 255, 255)); expectedImg.SetPixel(1, 1, Color.FromArgb(255, 0, 0, 0)); expectedImg.SetPixel(1, 2, Color.FromArgb(255, 255, 255, 255)); expectedImg.SetPixel(2, 0, Color.FromArgb(255, 255, 255, 255)); expectedImg.SetPixel(2, 1, Color.FromArgb(255, 0, 0, 0)); expectedImg.SetPixel(2, 2, Color.FromArgb(255, 255, 255, 255)); // Act var negative = new NegativeImage(img); var negativeImage = negative.AddNegativeFilter(); // Assert for (var x = 0; x < negativeImage.Width; x++) { for (var y = 0; y < negativeImage.Height; y++) { int expectedAlpha = img.GetPixel(x, y).A; var expectedRed = 255 - img.GetPixel(x, y).R; var expectedGreen = 255 - img.GetPixel(x, y).G; var expectedBlue = 255 - img.GetPixel(x, y).B; Assert.AreEqual(expectedAlpha, negativeImage.GetPixel(x, y).A); Assert.AreEqual(expectedRed, negativeImage.GetPixel(x, y).R); Assert.AreEqual(expectedGreen, negativeImage.GetPixel(x, y).G); Assert.AreEqual(expectedBlue, negativeImage.GetPixel(x, y).B); } } }