您的位置:首页 > 编程语言 > C#

C# Bitmap 复制

2013-10-17 13:26 309 查看
以后再详述,先上代码。

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) / 8;

if (bitdepth_per_pixel != 1 && bitdepth_per_pixel != 3 && bitdepth_per_pixel != 4)
{
return false;
}

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);

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 = 0; i < source_bitmapdata_height; i++)
{
for (int j = 0; 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; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: