Use Visual C # To process digital images (2)

Source: Internet
Author: User
[Introduction] This article uses a simple example to show you how to use Visual C # And GDI + to process digital images.
 
Note: To compile Insecure code, set the "allow Insecure code block" attribute on the project property page to true, as shown in the following figure:

 

The program implementation result of this function is as follows:

(Before processing)

(After processing)

 

The algorithm of the gray () function is as follows:

Public static bool gray (Bitmap B)

{

Bitmapdata bmdata = B. lockbits (New rectangle (0, 0, B. Width, B. Height ),

Imagelockmode. readwrite, pixelformat. format24bpprgb );

Int stride = bmdata. stride;

System. intptr scan0 = bmdata. scan0;

Unsafe

{

Byte * P = (byte *) (void *) scan0;

Int noffset = stride-B. Width * 3;

Byte red, green, blue;

For (INT y = 0; y
{

For (INT x = 0; x <B. width; ++ X)

{

Blue = P [0];

Green = P [1];

Red = P [2];

P [0] = P [1] = P [2] = (byte) (. 299 * red +. 587 * green +. 114 * Blue );

P + = 3;

}

P + = noffset;

}

}

B. unlockbits (bmdata );

Return true;

}

This function completes the grayscale processing of the image. Our basic idea is to take the average value of the three color components of each pixel point. However, due to the sensitivity of human eyes, it is not good to take the average value completely, so in the program I took three of the best parameters:. 299,. 587,. 114. However, it should be noted that in GDI +, the image storage format is BGR rather than RGB, that is, the order is blue, green, and red. Therefore, you must set the values of red, green, blue, and other variables in the for loop. If the function is successfully executed, true is returned.

The program implementation result of this function is as follows:

(Before processing)

(After processing)

The algorithm of the brightness () function is as follows:

Public static bool brightness (Bitmap B, int nbrightness)

{

If (nbrightness <-255 | nbrightness> 255)

Return false;

Bitmapdata bmdata = B. lockbits (New rectangle (0, 0, B. Width,

B. Height), imagelockmode. readwrite,

Pixelformat. format24bpprgb );

Int stride = bmdata. stride;

System. intptr scan0 = bmdata. scan0;

Int nval = 0;

Unsafe

{

Byte * P = (byte *) (void *) scan0;

Int noffset = stride-B. Width * 3;

Int nwidth = B. Width * 3;

For (INT y = 0; y
{

For (INT x = 0; x <nwidth; ++ X)

{

Nval = (INT) (p [0] + nbrightness );

If (nval <0) nval = 0;

If (nval> 255) nval = 255;

P [0] = (byte) nval;

++ P;

}

P + = noffset;

}

}

B. unlockbits (bmdata );

Return true;

}

This function is used to increase the brightness of the image. It has an addition parameter-nbrightness, Which is input by the user and ranges from-255 ~ 255. After the brightening parameter is obtained, the Unsafe code part of the function processes the different color components of each pixel point one by one, that is, a brightening parameter is added to the original value to obtain the new value. At the same time, there is an operation in the code to prevent the score from crossing the border, because the RGB score range is 0 ~ 255. You need to reset the value once the value exceeds this range. After the function is successfully executed, true must be returned.

The program implementation result of this function is as follows:

First, we set the image brightening parameter to 100 (the range is-255 ~ 255), and then execute the following results. You can try other parameter values.

(Before processing)

 

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.