. Net Image Processing

Source: Internet
Author: User
Reference:

Reverse Phase
Public static bool invert (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;
Int nwidth = B. Width * 3;
For (INT y = 0; y <B. height; ++ y)
{
For (INT x = 0; x <nwidth; ++ x screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>
{
P [0] = (byte) (255-p [0]);
++ P;
}
P + = noffset;
}
}

B. unlockbits (bmdata );
Return true;
}

Public static bool grayscale (Bitmap B)
{
// Black/white
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 <B. height; ++ y)
{
For (INT x = 0; x <B. width; ++ x screen. width/2) This. width = screen. width/2 "vspace = 2 border = 0>
{
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;
}

Brightness
Public static bool brightness (Bitmap B, int nbrightness)
{
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;
Int nwidth = B. Width * 3;
For (INT y = 0; y <B. height; ++ y)
{
For (INT x = 0; x <nwidth; ++ X)
{
Int 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;
}

Contrast
Public static bool contrast (Bitmap B, int ncontrast)
{
If (ncontrast <-100) return false;
If (ncontrast> 100) return false;
Double pixel = 0, contrast = (100.0 + ncontrast)/100.0;
Contrast * = contrast;
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;
Int nwidth = B. Width * 3;
For (INT y = 0; y <B. height; ++ y)
{
For (INT x = 0; x <nwidth; ++ X)
{
Byte color = P [0];
Pixel = color/255.0;
Pixel-= 0.5;
Pixel * = contrast;
Pixel + = 0.5;
Pixel * = 255;
If (pixel <0) pixel = 0;
If (pixel> 255) pixel = 255;
P [0] = (byte) pixel;
++ P;
}
P + = noffset;
}
}
B. unlockbits (bmdata );
Return true;
}

Public static bool gamma (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;

Byte [] redgamma = new byte [1, 256];
Byte [] greengamma = new byte [256];
Byte [] bluegamma = new byte [1, 256];

For (INT I = 0; I <256; ++ I)
{
Redgamma [I] = (byte) math. Min (255, (INT) (255.0
* Math. Pow (I/255.0, 1.0/5) + 0.5); // 5 of 1.0/5 is the red GAMMA value.
Greengamma [I] = (byte) math. Min (255, (INT) (255.0
* Math. Pow (I/255.0, 1.0/5) + 0.5 ));
Bluegamma [I] = (byte) math. Min (255, (INT) (255.0
* Math. Pow (I/255.0, 1.0/5) + 0.5 ));
}

Unsafe
{
Byte * P = (byte *) (void *) scan0;
Int noffset = stride-B. Width * 3;
Int nwidth = B. Width * 3;

Byte red, green, blue;
For (INT y = 0; y <B. height; ++ y)
{
For (INT x = 0; x <B. width; ++ X)
{
Blue = P [0];
Green = P [1];
Red = P [2];

P [0] = (byte) bluegamma [(INT) BLue];
P [1] = (byte) greengamma [(INT) Green];
P [2] = (byte) redgamma [(INT) Red];
P + = 3;
}
P + = noffset;
}
}

B. unlockbits (bmdata );
Return true;
}

Channel
Public static bool channel (Bitmap B, char RGB)
{
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;
Int nwidth = B. Width * 3;

Byte red, green, blue;
For (INT y = 0; y <B. height; ++ y)
{
For (INT x = 0; x <B. width; ++ X)
{
Switch (RGB)
{
Case 'r ':
P [0] = (byte) 0;
P [1] = (byte) 0;
Break;
Case 'G ':
P [0] = (byte) 0;
P [2] = (byte) 0;
Break;
Case 'B ':
P [2] = (byte) 0;
P [1] = (byte) 0;
Break;
}
P + = 3;
}
P + = noffset;
}
}

B. unlockbits (bmdata );

Return true;
}

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.