C # Image Processing (7)-adjust the brightness of an image

Source: Internet
Author: User

Back to the implementation of image-oriented pixel processing. Think about it. If I knew it, I would like to advance the "adjust the brightness of the image" part to Image Processing (6), because this part is similar to the principles of (2) to (5. But forget it. If you don't want to change it, write it down.

In my personal understanding, adjusting the brightness of an image is done by adding a constant to the red, green, and Blue values in each pixel. In fact, it is useless to describe more and paste the Code directly,

/// <Summary>
/// Adjust the image brightness
/// </Summary>
/// <Param name = "mybm"> original image </param>
/// <Param name = "width"> length of the original image </param>
/// <Param name = "height"> height of the original image </param>
/// <Param name = "val"> increase or decrease the light/dark value </param>
/// <Returns> the image after being dimmed </returns>
Public Bitmap LDPic (Bitmap mybm, int width, int height, int val)
{
Bitmap bm = new Bitmap (width, height); // Initialize an image object after record processing
Int x, y, resultR, resultG, and resultB; // x and y indicate the number of cycles, and the last three values indicate the values of red, green, and blue.
Color pixel;

For (x = 0; x <width; x ++)
{
For (y = 0; y {
Pixel = mybm. GetPixel (x, y); // obtain the value of the current pixel.
ResultR = helper. CheckRange (pixel. R + val); // check if the red value exceeds [0,255]
ResultG = helper. CheckRange (pixel. G + val); // check if the green value exceeds [0,255]
ResultB = helper. CheckRange (pixel. B + val); // check if the blue value exceeds [0,255]
Bm. SetPixel (x, y, Color. FromArgb (resultR, resultG, resultB); // plot
}
}

Return bm; // return the image after the light is dimmed.
}

 

The CheckRange function is used to check whether the modified red, green, and Blue values exceed the range [0,255]. As for this function, it is relatively simple, so I am sorry to post the code.

By the way, you can use the numericUpDown control to obtain the light and dark values, which is more convenient, because I did this, note that the numericUpDown value obtained through the numericUpDown control. value should be used to forcibly convert the int type, that is, (int) numericUpDown. value.

At last, you should use pictureBox to display the adjusted image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.