zoukankan      html  css  js  c++  java
  • C# Bitmap 复制

    以后再详述,先上代码。

     1 public bool CopyBitmap(Bitmap source, Bitmap destination)
     2 {
     3     if ((source.Width != destination.Width) || (source.Height != destination.Height) || (source.PixelFormat != destination.PixelFormat))
     4     {
     5         return false;
     6     }
     7 
     8     int bitdepth_per_pixel = Bitmap.GetPixelFormatSize(source.PixelFormat) / 8;
     9 
    10     if (bitdepth_per_pixel != 1 && bitdepth_per_pixel != 3 && bitdepth_per_pixel != 4)
    11     {
    12         return false;
    13     }
    14 
    15     BitmapData source_bitmapdata = null;
    16     BitmapData destination_bitmapdata = null;
    17 
    18     try
    19     {
    20         source_bitmapdata = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadWrite,
    21                                         source.PixelFormat);
    22         destination_bitmapdata = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.ReadWrite,
    23                                         destination.PixelFormat);
    24 
    25         int source_bitmapdata_bitdepth_width = source_bitmapdata.Width * bitdepth_per_pixel;
    26         int source_bitmapdata_height = source_bitmapdata.Height;
    27         int source_bitmapdata_bitdepth_stride = source_bitmapdata.Stride;
    28 
    29         unsafe
    30         {
    31             byte* source_ptr = (byte*)source_bitmapdata.Scan0;
    32             byte* destination_ptr = (byte*)destination_bitmapdata.Scan0;
    33 
    34             int offset = source_bitmapdata_bitdepth_stride - source_bitmapdata_bitdepth_width;
    35 
    36             for (int i = 0; i < source_bitmapdata_height; i++)
    37             {
    38                 for (int j = 0; j < source_bitmapdata_bitdepth_width; j++, source_ptr++, destination_ptr++)
    39                 {
    40                     *destination_ptr = *source_ptr;
    41                 }
    42 
    43                 source_ptr += offset;
    44                 destination_ptr += offset;
    45             }
    46         }
    47 
    48         source.UnlockBits(source_bitmapdata);
    49         destination.UnlockBits(destination_bitmapdata);
    50 
    51         return true;
    52     }
    53     catch { return false; }
    54 }
  • 相关阅读:
    Java实现 LeetCode 455 分发饼干
    Java实现 LeetCode 455 分发饼干
    Java实现 LeetCode 455 分发饼干
    Java实现 LeetCode 454 四数相加 II
    Java实现 LeetCode 454 四数相加 II
    Java实现 LeetCode 454 四数相加 II
    FFmpeg解码H264及swscale缩放详解
    linux中cat more less head tail 命令区别
    C语言字符串操作总结大全(超详细)
    如何使用eclipse进行嵌入式Linux的开发
  • 原文地址:https://www.cnblogs.com/daocaoren/p/3373964.html
Copyright © 2011-2022 走看看