I am creating a extremely basic image editor, and trying to set a value for a color (R,G,B) numerically. For example, here is an fragment of my code:
for (int row = 0; row < thePicture.Width; row = row + 1)
{
for (int col = 0; col < thePicture.Height; col = col + 1)
{
Color pixel = thePicture.GetPixel(row, col);
pixel = Color.FromArgb(5 + pixel.R, 5 + pixel.G, 5 + pixel.B);
//+5 is making the image darker... I think
if (pixel.R > 255)//This is used to prevent the program from crashing
{
pixel.R = //is this possible? or another way? I am intending
} //Make this 255
thePicture.SetPixel(row, col, pixel);
}
}
Take mind that it's in windows forum. Nothhing too advanced please, very basic understanding of C#. Thanks
From the MSDN article for System.Drawing.Color
's .R
property.
The property R
is readonly (only the getter is defined).
Thus, a new color must be created.
Try pixel = Color.FromArgb(pixel.A, Math.Min(255, pixel.R + 5), pixel.G, pixel.B);
Explaination:
We're creating a new color using the previous color's A (Alpha), R (Red), G (Green), and B (Blue) property values.
However, in the case for R, instead of passing in the previous R value, we pass the adjusted R value. You can use Math.Min(x,y)
to ensure the "brightened" R value doesn't exceed the maximum 255 value