Exemple #1
0
        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);
            }
        }
Exemple #2
0
        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 AddGrayscaleFilter_FilterAdded_ColorsGrayscale()
        {
            // Arrange
            var       r           = new Random();
            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       red   = 200;
                    var       green = 100;
                    var       blue  = 0;
                    if (y % 2 == 0)
                    {
                        red  = 0;
                        blue = 200;
                    }

                    img.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
                }
            }

            expectedImg.SetPixel(0, 0, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(0, 1, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(0, 2, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(1, 0, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(1, 1, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(1, 2, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(2, 0, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(2, 1, Color.FromArgb(255, 100, 100, 100));
            expectedImg.SetPixel(2, 2, Color.FromArgb(255, 100, 100, 100));


            // Act
            var gray      = new GrayscaleImage(img);
            var grayImage = gray.AddGrayscaleFilter();

            // Assert
            for (var x = 0; x < grayImage.Width; x++)
            {
                for (var y = 0; y < grayImage.Height; y++)
                {
                    int expectedAlpha = img.GetPixel(x, y).A;
                    var expectedRed   = expectedImg.GetPixel(x, y).R;
                    var expectedGreen = expectedImg.GetPixel(x, y).G;
                    var expectedBlue  = expectedImg.GetPixel(x, y).B;

                    Assert.AreEqual(expectedAlpha, grayImage.GetPixel(x, y).A);
                    Assert.AreEqual(expectedRed, grayImage.GetPixel(x, y).R);
                    Assert.AreEqual(expectedGreen, grayImage.GetPixel(x, y).G);
                    Assert.AreEqual(expectedBlue, grayImage.GetPixel(x, y).B);
                }
            }
        }