C# Bitmap 复制 C# Bitmap 复制的小例子

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C# Bitmap 复制 C# Bitmap 复制的小例子

  2021-03-20 我要评论
想了解C# Bitmap 复制的小例子的相关内容吗,在本文为您仔细讲解C# Bitmap 复制的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;
    }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们