当前位置: 移动技术网 > IT编程>开发语言>c# > C# Bitmap 复制的小例子

C# Bitmap 复制的小例子

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:public bitmap copybitmap(bitmap source){    int depth = bitma

复制代码 代码如下:

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网