// Pixel method, about 400 milliseconds
Private void pixel_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Color curcolor;
Int ret;
For (INT I = 0; I <curbitmap. width; I ++)
{
For (Int J = 0; j <curbitmap. height; j ++)
{
Curcolor = curbitmap. getpixel (I, j );
Ret = (INT) (curcolor. R * 0.299 + curcolor. g * 0.587 + curcolor. B * 0.114 );
Curbitmap. setpixel (I, j, color. fromargb (Ret, RET, RET ));
}
}
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}
// Memory method, which is about 2.5 ms.
Private void memory_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Rectangle rect = new rectangle (0, 0, curbitmap. Width, curbitmap. Height );
System. Drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. Drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat );
Intptr = BMP data. scan0;
Int bytes = curbitmap. Width * curbitmap. Height * 3;
Byte [] rgbvalues = new byte [bytes];
System. runtime. interopservices. Marshal. Copy (PTR, rgbvalues, 0, bytes );
Double colortemp = 0;
For (INT I = 0; I <rgbvalues. length; I + = 3)
{
Colortemp = rgbvalues [I + 2] * 0.299 + rgbvalues [I + 1] * 0.587 + rgbvalues [I] * 0.114;
Rgbvalues [I] = rgbvalues [I + 1] = rgbvalues [I + 2] = (byte) colortemp;
}
System. runtime. interopservices. Marshal. Copy (rgbvalues, 0, PTR, bytes );
Curbitmap. unlockbits (BMP data );
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}
// This method is the fastest, about 1.7 milliseconds.
Private void pointer_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Rectangle rect = new rectangle (0, 0, curbitmap. Width, curbitmap. Height );
System. Drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. Drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat );
Byte temp = 0;
Unsafe
{
Byte * PTR = (byte *) (BMP data. scan0 );
For (INT I = 0; I <BMP data. height; I ++)
{
For (Int J = 0; j <BMP data. width; j ++)
{
Temp = (byte) (0.299 * PTR [2] + 0.587 * PTR [1] + 0.114 * PTR [0]);
PTR [0] = PTR [1] = PTR [2] = temp;
PTR + = 3;
}
PTR + = BMP data. stride-BMP data. Width * 3;
}
}
Curbitmap. unlockbits (BMP data );
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}
Http://hi.baidu.com/goga