C# Bitmap 复制

时间:2024-01-04 18:53:50

以后再详述,先上代码。

 public bool CopyBitmap(Bitmap source, Bitmap destination)
{
if ((source.Width != destination.Width) || (source.Height != destination.Height) || (source.PixelFormat != destination.PixelFormat))
{
return false;
} int bitdepth_per_pixel = Bitmap.GetPixelFormatSize(source.PixelFormat) / ; if (bitdepth_per_pixel != && bitdepth_per_pixel != && bitdepth_per_pixel != )
{
return false;
} BitmapData source_bitmapdata = null;
BitmapData destination_bitmapdata = null; try
{
source_bitmapdata = source.LockBits(new Rectangle(, , source.Width, source.Height), ImageLockMode.ReadWrite,
source.PixelFormat);
destination_bitmapdata = destination.LockBits(new Rectangle(, , destination.Width, destination.Height), ImageLockMode.ReadWrite,
destination.PixelFormat); int source_bitmapdata_bitdepth_width = source_bitmapdata.Width * bitdepth_per_pixel;
int source_bitmapdata_height = source_bitmapdata.Height;
int source_bitmapdata_bitdepth_stride = source_bitmapdata.Stride; unsafe
{
byte* source_ptr = (byte*)source_bitmapdata.Scan0;
byte* destination_ptr = (byte*)destination_bitmapdata.Scan0; int offset = source_bitmapdata_bitdepth_stride - source_bitmapdata_bitdepth_width; for (int i = ; i < source_bitmapdata_height; i++)
{
for (int j = ; j < source_bitmapdata_bitdepth_width; j++, source_ptr++, destination_ptr++)
{
*destination_ptr = *source_ptr;
} source_ptr += offset;
destination_ptr += offset;
}
} source.UnlockBits(source_bitmapdata);
destination.UnlockBits(destination_bitmapdata); return true;
}
catch { return false; }
}