/// <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;
}
}