C# Bitmap 复制的小例子
public bitmap copybitmap(bitmap source)
{
int depth = bitmap.getpixelformatsize(source.pixelformat);
if (depth != 8 && depth != 24 && depth != 32)
{
return null;
}
bitmap destination = new bitmap(source.width, source.height, source.pixelformat);
bitmapdata source_bitmapdata = null;
bitmapdata destination_bitmapdata = null;
try
{
source_bitmapdata = source.lockbits(new rectangle(0, 0, source.width, source.height), imagelockmode.readwrite,
source.pixelformat);
destination_bitmapdata = destination.lockbits(new rectangle(0, 0, destination.width, destination.height), imagelockmode.readwrite,
destination.pixelformat);
unsafe
{
byte* source_ptr = (byte*)source_bitmapdata.scan0;
byte* destination_ptr = (byte*)destination_bitmapdata.scan0;
for (int i = 0; i < (source.width * source.height * (depth / 8)); i++)
{
*destination_ptr = *source_ptr;
source_ptr++;
destination_ptr++;
}
}
source.unlockbits(source_bitmapdata);
destination.unlockbits(destination_bitmapdata);
return destination;
}
catch
{
destination.dispose();
return null;
}
}