Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.IO;
- namespace ImageProcessing
- {
- internal class Program
- {
- // Path to the input and output image files
- // The file should be located where the .exe application is
- public static string imagePath = $@"image.jpg";
- public static string newImagePath = $@"newImage.jpg";
- public static void Main(string[] args)
- {
- // Loading the image from the file
- Bitmap image = LoadImage(imagePath);
- // Checking if the image was loaded successfully
- bool test = (image != null);
- string testResult = test ? "successfully" : "unsuccessfully";
- Console.WriteLine($"Loading image: {imagePath}...\n " +
- $"... completed with {testResult}\n");
- int choice;
- while (true && test)
- {
- Console.WriteLine(
- "Choose an option:\n" +
- "[1] Brighten the image\n" +
- "[2] Darken the image\n" +
- "[3] Swap colors\n" +
- "[0] Exit");
- choice = int.Parse(Console.ReadLine());
- Console.Clear();
- switch (choice)
- {
- case 1:
- //BrightenImage(image);
- ChangeImageBrightness(image, 50);
- break;
- case 2:
- //DarkenImage(image);
- ChangeImageBrightness(image, -50);
- break;
- case 3:
- SwapColors(image);
- break;
- case 0:
- Console.WriteLine("Exiting the application");
- return;
- default:
- Console.WriteLine("Invalid operation");
- break;
- }
- }
- Console.ReadKey();
- }
- // Method to brighten the image
- public static void BrightenImage(Bitmap img)
- {
- int brighteningStrength = 50;
- // Iterating through each pixel of the image
- for (int i = 0; i < img.Width; i++)
- {
- for (int j = 0; j < img.Height; j++)
- {
- // Retrieving the pixel value
- Color pixel = img.GetPixel(i, j);
- // Changing the brightness of the pixel by 50 units
- Color newPixel = Color.FromArgb(
- ClampValue(pixel.R + brighteningStrength),
- ClampValue(pixel.G + brighteningStrength),
- ClampValue(pixel.B + brighteningStrength));
- // Setting the new pixel value
- img.SetPixel(i, j, newPixel);
- }
- }
- Console.WriteLine("...Brightened \n\n");
- // Saving the modified image
- img.Save(newImagePath);
- }
- public static void DarkenImage(Bitmap img)
- {
- int darkeningStrength = 50;
- for (int i = 0; i < img.Width; i++)
- {
- for (int j = 0; j < img.Height; j++)
- {
- // Retrieving the pixel value
- Color pixel = img.GetPixel(i, j);
- // Changing the brightness of the pixel by 50 units
- Color newPixel = Color.FromArgb(
- ClampValue(pixel.R - darkeningStrength),
- ClampValue(pixel.G - darkeningStrength),
- ClampValue(pixel.B - darkeningStrength));
- // Setting the new pixel value
- img.SetPixel(i, j, newPixel);
- }
- }
- Console.WriteLine("...Darkened \n\n");
- // Saving the modified image
- img.Save(newImagePath);
- }
- public static void ChangeImageBrightness(Bitmap img, int modificationStrength)
- {
- for (int i = 0; i < img.Width; i++)
- {
- for (int j = 0; j < img.Height; j++)
- {
- // Retrieving the pixel value
- Color pixel = img.GetPixel(i, j);
- // Changing the brightness of the pixel by the specified amount
- Color newPixel = Color.FromArgb(
- ClampValue(pixel.R + modificationStrength),
- ClampValue(pixel.G + modificationStrength),
- ClampValue(pixel.B + modificationStrength));
- // Setting the new pixel value
- img.SetPixel(i, j, newPixel);
- }
- }
- Console.WriteLine("...Brightness changed \n\n");
- // Saving the modified image
- img.Save(newImagePath);
- }
- public static void SwapColors(Bitmap img)
- {
- Color colorToReplace, newColor;
- // Getting colors from the user
- Console.WriteLine("Enter the value of the color to be replaced in RGB format. Provide three values from the range 0-255, separated by commas (,).");
- colorToReplace = ConvertStringToColor(Console.ReadLine());
- Console.WriteLine("Enter the value of the new color in RGB format. Provide three values from the range 0-255, separated by commas (,).");
- newColor = ConvertStringToColor(Console.ReadLine());
- // Loop to iterate over each pixel and replace the matching color
- int modifiedPixelCount = 0;
- for (int i = 0; i < img.Width; i++)
- {
- for (int j = 0; j < img.Height; j++)
- {
- Color color = img.GetPixel(i, j);
- if (color == colorToReplace)
- {
- img.SetPixel(i, j, newColor);
- modifiedPixelCount++;
- }
- }
- }
- Console.WriteLine($"Replaced {modifiedPixelCount} pixels.\n\n");
- // Saving the modified image
- img.Save(newImagePath);
- }
- // Method to convert user-input RGB values to a Color object
- public static Color ConvertStringToColor(string rgb)
- {
- int r, g, b;
- try
- {
- string[] rgbComponents = rgb.Split(',');
- r = int.Parse(rgbComponents[0]);
- g = int.Parse(rgbComponents[1]);
- b = int.Parse(rgbComponents[2]);
- return Color.FromArgb(r, g, b);
- }
- catch (Exception e)
- {
- Console.WriteLine("Invalid color input. Setting default color (black).");
- return Color.Black;
- }
- }
- // Method to prevent values from exceeding the 0-255 (RGB) range by clamping the variables
- public static int ClampValue(int value)
- {
- if (value < 0) return 0;
- if (value > 255) return 255;
- return value;
- }
- // Method to load the image from the given path and display a message if there's an issue
- public static Bitmap LoadImage(string filePath)
- {
- if (File.Exists(filePath) == false) return null;
- try
- {
- Bitmap bitmap = new Bitmap(filePath);
- return bitmap;
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement