View difference between Paste ID: VVraWQ2G and LLZ1CV2v
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Drawing;
3
using System.IO;
4
5
namespace ImageProcessing
6
{
7
    internal class Program
8
    {
9
        // Path to the input and output image files
10
        // The file should be located where the .exe application is
11
        public static string imagePath = $@"image.jpg";
12
        public static string newImagePath = $@"newImage.jpg";
13
14
        public static void Main(string[] args)
15
        {
16
            // Loading the image from the file
17
            Bitmap image = LoadImage(imagePath);
18
19
            // Checking if the image was loaded successfully
20
            bool test = (image != null);
21
            string testResult = test ? "successfully" : "unsuccessfully";
22
23
            Console.WriteLine($"Loading image: {imagePath}...\n " +
24
                $"... completed with {testResult}\n");
25
26
            int choice;
27
            while (true && test)
28
            {
29
                Console.WriteLine(
30
                        "Choose an option:\n" +
31
                        "[1] Brighten the image\n" +
32
                        "[2] Darken the image\n" +
33
                        "[3] Swap colors\n" +
34
                        "[0] Exit");
35
36
                choice = int.Parse(Console.ReadLine());
37
38
                Console.Clear();
39
                switch (choice)
40
                {
41
                    case 1:
42
                        //BrightenImage(image);
43
                        ChangeImageBrightness(image, 50);
44
                        break;
45
                    case 2:
46
                        //DarkenImage(image);
47
                        ChangeImageBrightness(image, -50);
48
                        break;
49
                    case 3:
50
                        SwapColors(image);
51
                        break;
52
                    case 0:
53
                        Console.WriteLine("Exiting the application");
54
                        return;
55
                    default:
56
                        Console.WriteLine("Invalid operation");
57
                        break;
58
                }
59
            }
60
61
            Console.ReadKey();
62
        }
63
64
        // Method to brighten the image
65
        public static void BrightenImage(Bitmap img)
66
        {
67
            int brighteningStrength = 50;
68
            // Iterating through each pixel of the image
69
            for (int i = 0; i < img.Width; i++)
70
            {
71
                for (int j = 0; j < img.Height; j++)
72
                {
73
                    // Retrieving the pixel value
74
                    Color pixel = img.GetPixel(i, j);
75
76
                    // Changing the brightness of the pixel by 50 units
77
                    Color newPixel = Color.FromArgb(
78
                        ClampValue(pixel.R + brighteningStrength),
79
                        ClampValue(pixel.G + brighteningStrength),
80
                        ClampValue(pixel.B + brighteningStrength));
81
82
                    // Setting the new pixel value
83
                    img.SetPixel(i, j, newPixel);
84
                }
85
            }
86
87
            Console.WriteLine("...Brightened \n\n");
88
            // Saving the modified image
89
            img.Save(newImagePath);
90
        }
91
92
        public static void DarkenImage(Bitmap img)
93
        {
94
            int darkeningStrength = 50;
95
            for (int i = 0; i < img.Width; i++)
96
            {
97
                for (int j = 0; j < img.Height; j++)
98
                {
99
                    // Retrieving the pixel value
100
                    Color pixel = img.GetPixel(i, j);
101
102
                    // Changing the brightness of the pixel by 50 units
103
                    Color newPixel = Color.FromArgb(
104
                        ClampValue(pixel.R - darkeningStrength),
105
                        ClampValue(pixel.G - darkeningStrength),
106
                        ClampValue(pixel.B - darkeningStrength));
107
108
                    // Setting the new pixel value
109
                    img.SetPixel(i, j, newPixel);
110
                }
111
            }
112
113
            Console.WriteLine("...Darkened \n\n");
114
            // Saving the modified image
115
            img.Save(newImagePath);
116
        }
117
118
        public static void ChangeImageBrightness(Bitmap img, int modificationStrength)
119
        {
120
            for (int i = 0; i < img.Width; i++)
121
            {
122
                for (int j = 0; j < img.Height; j++)
123
                {
124
                    // Retrieving the pixel value
125
                    Color pixel = img.GetPixel(i, j);
126
127
                    // Changing the brightness of the pixel by the specified amount
128
                    Color newPixel = Color.FromArgb(
129
                        ClampValue(pixel.R + modificationStrength),
130
                        ClampValue(pixel.G + modificationStrength),
131
                        ClampValue(pixel.B + modificationStrength));
132
133
                    // Setting the new pixel value
134
                    img.SetPixel(i, j, newPixel);
135
                }
136
            }
137
138
            Console.WriteLine("...Brightness changed \n\n");
139
            // Saving the modified image
140
            img.Save(newImagePath);
141
        }
142
143
        public static void SwapColors(Bitmap img)
144
        {
145
            Color colorToReplace, newColor;
146
147
            // Getting colors from the user
148
            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 (,).");
149
            colorToReplace = ConvertStringToColor(Console.ReadLine());
150
151
            Console.WriteLine("Enter the value of the new color in RGB format. Provide three values from the range 0-255, separated by commas (,).");
152
            newColor = ConvertStringToColor(Console.ReadLine());
153
154
            // Loop to iterate over each pixel and replace the matching color
155
            int modifiedPixelCount = 0;
156
            for (int i = 0; i < img.Width; i++)
157
            {
158
                for (int j = 0; j < img.Height; j++)
159
                {
160
                    Color color = img.GetPixel(i, j);
161
162
                    if (color == colorToReplace)
163
                    {
164
                        img.SetPixel(i, j, newColor);
165
                        modifiedPixelCount++;
166
                    }
167
                }
168
            }
169
170
            Console.WriteLine($"Replaced {modifiedPixelCount} pixels.\n\n");
171
172
            // Saving the modified image
173
            img.Save(newImagePath);
174
        }
175
176
        // Method to convert user-input RGB values to a Color object
177
        public static Color ConvertStringToColor(string rgb)
178
        {
179
            int r, g, b;
180
181
            try
182
            {
183
                string[] rgbComponents = rgb.Split(',');
184
                r = int.Parse(rgbComponents[0]);
185
                g = int.Parse(rgbComponents[1]);
186
                b = int.Parse(rgbComponents[2]);
187
                return Color.FromArgb(r, g, b);
188
            }
189
            catch (Exception e)
190
            {
191
                Console.WriteLine("Invalid color input. Setting default color (black).");
192
                return Color.Black;
193
            }
194
        }
195
196
        // Method to prevent values from exceeding the 0-255 (RGB) range by clamping the variables
197
        public static int ClampValue(int value)
198
        {
199
            if (value < 0) return 0;
200
            if (value > 255) return 255;
201
202
            return value;
203
        }
204
205
        // Method to load the image from the given path and display a message if there's an issue
206
        public static Bitmap LoadImage(string filePath)
207
        {
208
            if (File.Exists(filePath) == false) return null;
209
210
            try
211
            {
212
                Bitmap bitmap = new Bitmap(filePath);
213
                return bitmap;
214
            }
215
            catch (Exception e)
216
            {
217
                Console.WriteLine(e);
218
                return null;
219
            }
220
        }
221
    }
222
}
223