基于GetPixel 和 SetPixel 速度实在是太慢,处理大点的图片几乎崩溃的速度,幸好有解决方案~
Bitmap^ bmp = gcnew Bitmap( "y.bmp" );
Rectangle rect = Rectangle(0,0,bmp->Width,bmp->Height);
System::Drawing::Imaging::BitmapData^ bmpData = bmp->LockBits( rect, System::Drawing::Imaging::ImageLockMode::ReadWrite, bmp->PixelFormat );
// Get the address of the first line.
IntPtr ptr = bmpData->Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmpData->Stride * bmp->Height ;
array<Byte>^rgbValues = gcnew array<Byte>(bytes);
// Copy the RGB values into the array.
// System::Runtime::InteropServices::Marshal::Copy( ptr, rgbValues, 0, bytes );
int PixelSize=3;
// Set every third value to 255.
for ( int y = 0; y < bmp->Height; y++ )
{
for ( int x = 0; x < bmp->Width; x ++ )
{
rgbValues[bmpData->Stride * y + 3*x] = 200;
rgbValues[bmpData->Stride * y + 3*x + 1] = 200;
rgbValues[bmpData->Stride * y + 3*x + 2] = 200;
}
}
// Copy the RGB values back to the bitmap
System::Runtime::InteropServices::Marshal::Copy( rgbValues, 0, ptr, bytes );
// Unlock the bits.
bmp->UnlockBits( bmpData );
picBox->Image = bmp;
String^ filename = L"a.jpg";
bmp->Save(filename);