C#BitMap8位/24位转Hobject通用方法

C#BitMap8位/24位转Hobject通用方法

 1    public void BitmapToHobject(Bitmap bitmap, out HObject image)
 2         {
 3             int height = bitmap.Height;//图像的高度
 4             int width = bitmap.Width;//图像的宽度
 5 
 6 
 7             Rectangle imgRect = new Rectangle(0, 0, width, height);
 8             BitmapData bitData = bitmap.LockBits(imgRect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
 9 
10 
11             //由于Bitmap图像每行的字节数必须保持为4的倍数,因此在行的字节数不满足这个条件时,会对行进行补充,步幅数Stride表示的就是补充过后的每行的字节数,也成为扫描宽度
12             int stride = bitData.Stride;
13             switch (bitmap.PixelFormat)
14             {
15                 case PixelFormat.Format8bppIndexed:
16                     {
17                         unsafe
18                         {
19                             int count = height * width;
20                             byte[] data = new byte[count];
21                             byte* bptr = (byte*)bitData.Scan0;
22                             fixed (byte* pData = data)
23                             {
24                                 for (int i = 0; i < height; i++)
25                                 {
26                                     for (int j = 0; j < width; j++)
27                                     {
28                                         /*
29                                          * 
30                                          如果直接使用GenImage1,传入BitData的Scan0(图像首元素的指针)作为内存指针的话,如果图像不满足行为4的倍数,那么填充的部分也会参与进来,从而导致图像扭曲
31                                          * 
32                                          * 
33                                          */
34 
35 
36                                         //舍去填充的部分
37                                         data[i * width + j] = bptr[i * stride + j];
38                                     }
39 
40                                 }
41                                 HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
42                             }
43                         }
44 
45                     }
46                     break;
47                 case PixelFormat.Format24bppRgb:
48                     {
49                         unsafe
50                         {
51                             int count = height * width * 3;//24位的BitMap每个像素三个字节
52                             byte[] data = new byte[count];
53                             byte* bptr = (byte*)bitData.Scan0;
54                             fixed (byte* pData = data)
55                             {
56                                 for (int i = 0; i < height; i++)
57                                 {
58                                     for (int j = 0; j < width * 3; j++)
59                                     {
60                                         //每个通道的像素需一一对应
61                                         data[i * width * 3 + j] = bptr[i * stride + j];
62                                     }
63                                 }
64                                 HOperatorSet.GenImageInterleaved(out image, new IntPtr(pData), "bgr", bitmap.Width, bitmap.Height, 0, "byte", bitmap.Width, bitmap.Height, 0, 0, -1, 0);
65                             }
66                         }
67                     }
68                     break;
69                 default:
70                     {
71                         unsafe
72                         {
73                             int count = height * width;
74                             byte[] data = new byte[count];
75                             byte* bptr = (byte*)bitData.Scan0;
76                             fixed (byte* pData = data)
77                             {
78                                 for (int i = 0; i < height; i++)
79                                 {
80                                     for (int j = 0; j < width; j++)
81                                     {
82                                         data[i * width + j] = bptr[i * stride + j];
83                                     }
84 
85                                 }
86                                 HOperatorSet.GenImage1(out image, "byte", width, height, new IntPtr(pData));
87                             }
88                         }
89 
90                     }
91                     break;
92             }
93             bitmap.UnlockBits(bitData);
94         }
95     }

 

posted @ 2020-07-01 15:18  木乔ni  阅读(1367)  评论(0编辑  收藏  举报