You are on page 1of 2

using System;

using System.Drawing;
using System.Windows.Forms;

namespace PictureBrightnessAdjuster
{
public partial class MainForm : Form
{
private Image originalImage;

public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)


{
// Load the image into the PictureBox
originalImage = Image.FromFile("example.jpg");
pictureBox.Image = originalImage;
}

private void trackBarBrightness_ValueChanged(object sender, EventArgs e)


{
// Update the brightness label with the current trackbar value
labelBrightnessValue.Text = trackBarBrightness.Value.ToString();

// Adjust the brightness of the image


Image adjustedImage = AdjustBrightness(originalImage,
trackBarBrightness.Value);
pictureBox.Image = adjustedImage;
}

private Image AdjustBrightness(Image image, int brightness)


{
// Create a copy of the original image
Bitmap bitmap = new Bitmap(image);

// Adjust the brightness of each pixel in the image


for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);

int newR = Clamp(pixelColor.R + brightness, 0, 255);


int newG = Clamp(pixelColor.G + brightness, 0, 255);
int newB = Clamp(pixelColor.B + brightness, 0, 255);

Color newColor = Color.FromArgb(newR, newG, newB);


bitmap.SetPixel(x, y, newColor);
}
}

return bitmap;
}

private int Clamp(int value, int min, int max)


{
if (value < min) return min;
if (value > max) return max;
return value;
}
}
}

You might also like