C# Bitmap 复制的小例子

时间:2023-01-19 12:49:39 


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

标签:C#,Bitmap,复制
0
投稿

猜你喜欢

  • c#实现KTV点歌系统

    2023-10-17 23:54:56
  • Android实现中国象棋游戏(局域网版)

    2023-11-22 16:07:07
  • Android语音声波控件 Android条形波控件

    2023-10-29 02:03:05
  • C++ 让函数返回数组的方法

    2022-12-04 06:49:33
  • 基于Java实现的图的广度优先遍历算法

    2021-06-02 06:51:20
  • 解决idea web项目中out目录更新不同步问题

    2023-01-30 01:32:28
  • c++动态内存管理详解(new/delete)

    2022-07-09 11:53:26
  • 谈谈RxJava2中的异常及处理方法

    2023-05-12 09:18:11
  • 详解Mybatis通用Mapper介绍与使用

    2023-11-29 08:49:08
  • Java特性之注解和异常 Throwable

    2023-04-22 13:14:53
  • Struts和servlet不能共存问题解决方法

    2022-09-20 07:26:59
  • 一篇文章让你彻底了解Java可重入锁和不可重入锁

    2023-12-06 11:57:26
  • java ThreadGroup的作用及方法详解

    2022-02-03 16:49:01
  • Java数据结构之优先级队列(堆)图文详解

    2021-06-25 13:47:58
  • Treeview动态添加用户控件传值和取值的实例代码

    2021-07-20 11:20:07
  • C#检测是否有u盘插入的方法

    2023-11-19 13:11:00
  • Android中设置组件半透明和透明的效果示例

    2023-05-07 18:16:04
  • MyBatis通过JDBC数据驱动生成的执行语句问题

    2023-08-24 08:42:18
  • C#端口转发用法详解

    2022-09-05 08:47:08
  • C# SMTP发送邮件的示例

    2021-06-20 12:36:10
  • asp之家 软件编程 m.aspxhome.com