C # Image Processing: blurred images

Source: Internet
Author: User

/// <Summary>
/// Blur the image
/// </Summary>
/// <Param name = "bitmap"> original image </param>
/// <Returns> blurred image </returns>
Public static bitmap blur (Bitmap bitmap)
{

If (Bitmap = NULL)
{
Return NULL;
}

Int width = bitmap. width;
Int Height = bitmap. height;

Try
{
Bitmap BMP return = new Bitmap (width, height, pixelformat. format24bpprgb );
Bitmapdata srcbits = bitmap. lockbits (New rectangle (0, 0, width, height), imagelockmode. readonly, pixelformat. format24bpprgb );
Bitmapdata targetbits = BMP return. lockbits (New rectangle (0, 0, width, height), imagelockmode. writeonly, pixelformat. format24bpprgb );

Unsafe
{
Byte * psrcbits = (byte *) srcbits. scan0.topointer ();
Byte * ptargetbits = (byte *) targetbits. scan0.topointer ();
Int stride = srcbits. stride;
Byte * ptemp;

For (INT y = 0; y {
For (INT x = 0; x <width; X ++)
{
If (x = 0 | x = width-1 | Y = 0 | Y = height-1)
{
// The pixel at the edge is not processed
Ptargetbits [0] = psrcbits [0];
Ptargetbits [1] = psrcbits [1];
Ptargetbits [2] = psrcbits [2];
}
Else
{
// Obtain the value of 9 around
Int R1, R2, R3, R4, R5, R6, R7, R8, R9;
Int G1, G2, G3, G4, G5, G6, G7, G8, G9;
Int B1, B2, B3, B4, B5, B6, B7, B8, B9;

Float FR, FG, FB;

// Top left
Ptemp = psrcbits-stride-3;
R1 = ptemp [2];
G1 = ptemp [1];
B1 = ptemp [0];

// Forward
Ptemp = psrcbits-stride;
R2 = ptemp [2];
G2 = ptemp [1];
B2 = ptemp [0];

// Upper right
Ptemp = psrcbits-stride + 3;
R3 = ptemp [2];
G3 = ptemp [1];
B3 = ptemp [0];

// Left
Ptemp = psrcbits-3;
R4 = ptemp [2];
G4 = ptemp [1];
B4 = ptemp [0];

// Right
Ptemp = psrcbits + 3;
R5 = ptemp [2];
G5 = ptemp [1];
B5 = ptemp [0];

// Bottom right
Ptemp = psrcbits + stride-3;
R6 = ptemp [2];
G6 = ptemp [1];
B6 = ptemp [0];

// Bottom
Ptemp = psrcbits + stride;
R7 = ptemp [2];
G7 = ptemp [1];
B7 = ptemp [0];

// Bottom right
Ptemp = psrcbits + stride + 3;
R8 = ptemp [2];
G8 = ptemp [1];
B8 = ptemp [0];

// Yourself
Ptemp = psrcbits;
R9 = ptemp [2];
G9 = ptemp [1];
B9 = ptemp [0];

Fr = (float) (R1 + r2 + R3 + R4 + R5 + R6 + R7 + R8 + R9 );
Fg = (float) (G1 + G2 + G3 + G4 + G5 + G6 + G7 + G8 + G9 );
Fb = (float) (b1 + B2 + B3 + B4 + B5 + B6 + B7 + B8 + B9 );

Fr/= 9;
FG/= 9;
FB/= 9;

Ptargetbits [0] = (byte) FB;
Ptargetbits [1] = (byte) FG;
Ptargetbits [2] = (byte) fr;

}

Psrcbits + = 3;
Ptargetbits + = 3;
}

Psrcbits + = srcbits. stride-width * 3;
Ptargetbits + = srcbits. stride-width * 3;
}
}

Bitmap. unlockbits (srcbits );
BMP return. unlockbits (targetbits );

Return BMP return;
}
Catch
{
Return NULL;
}

}

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.