以后再详述,先上代码。

 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 }

 

posted on 2013-10-17 13:26  盗艹人  阅读(3535)  评论(0编辑  收藏  举报