-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitmapImage.cs
112 lines (92 loc) · 2.98 KB
/
BitmapImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace HAF_2_0
{
/// <summary>
/// 图像信息类
/// </summary>
public class BitmapImage
{
private Bitmap image;
/// <summary>
/// 宽度
/// </summary>
public int Width{get;set;}
/// <summary>
/// 高度
/// </summary>
public int Height{get;set;}
/// <summary>
/// 格式(默认513)
/// </summary>
public int Format { get; set; }
/// <summary>
/// 图像数据
/// </summary>
public byte[] ImageData { get; set; }
/// <summary>
/// 使用Bitmap构造
/// </summary>
/// <param name="image"></param>
public BitmapImage(Bitmap image)
{
this.image = image;
}
/// <summary>
/// 图像数据构造
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="imageData"></param>
public BitmapImage(int width, int height, byte[] imageData)
{
this.Width = width;
this.Height = height;
this.Format = 0x201;
this.ImageData = imageData;
}
/// <summary>
/// 解析图片的信息
/// </summary>
public void ParseImage()
{
if (image != null)
{
int tempwidth = 0;
if (image.Width % 4 != 0)
{
tempwidth = image.Width / 4 * 4;
}
else
{
tempwidth = image.Width;
}
BitmapData data = image.LockBits(new Rectangle(0, 0, tempwidth, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr ptr = data.Scan0;
int soureBitArrayLength = data.Height * Math.Abs(data.Stride);
byte[] sourceBitArray = new byte[soureBitArrayLength];
//将bitmap中的内容拷贝到ptr_bgr数组中
Marshal.Copy(ptr, sourceBitArray, 0, soureBitArrayLength);
//Marshal.FreeHGlobal(ptr);
Width = data.Width;
Height = data.Height;
Format = 0x201;
int pitch = Math.Abs(data.Stride);
int line = Width * 3;
int bgr_len = line * Height;
ImageData = new byte[bgr_len];
for (int i = 0; i < Height; ++i)
{
Array.Copy(sourceBitArray, i * pitch, ImageData, i * line, line);
}
image.UnlockBits(data);
}
}
}
}