1
2 # ifndef _ bitmap_h
3 # DEFINE _ bitmap_h
4
5 # include <windows. h>
6
7 void saveimage (const char * BMP _file, void * rgb_data, int rgb_len, bitmapinfoheader * bi );
8 void fillbitmapinfohdr (bitmapinfoheader * bi, int width, int height, int deep );
9 bool getimagedata (hwnd target, rect, void ** data, int * Len );
10 bool getimagedata2 (hwnd target, rect, void * data, int * Len );
11 void captureimage (hwnd target, const char * destfile );
12 Void drawbitmap (hwnd, int width, int height, char * data, int deep );
13 void drawbitmap2 (hwnd, rect, char * data, int deep );
14 void stretchdrawbitmap (hwnd, int width, int height, char * data, int deep );
15 # endif
16 1 # include <windows. h>
2 # include <stdio. h>
3
4 void saveimage (const char * BMP _file, void * rgb_data, int rgb_len, bitmapinfoheader * bi) {// save bitmap as a file
5 bitmapfileheader HDR;
6 file * fsave;
7 memset (& HDR, 0, sizeof (HDR ));
8 HDR. bftype = (Word) ('M' <8) | 'B ');
9 HDR. bfsize = sizeof (HDR) + sizeof (bitmapinfoheader) + rgb_len;
10 HDR. bfoffbits = (DWORD) (sizeof (HDR) + sizeof (bitmapinfoheader ));
11 //--
12 fsave = fopen (BMP _file, "WB ");
13 fwrite (& HDR, sizeof (HDR), 1, fsave );
14 fwrite (Bi, sizeof (bitmapinfoheader), 1, fsave );
15 fwrite (rgb_data, rgb_len, 1, fsave );
16 fflush (fsave );
17 fclose (fsave );
18}
19
20
21 void fillbitmapinfohdr (bitmapinfoheader * bi, int width, int height, int deep) {// fill in Bitmap header information
22 memset (Bi, 0, sizeof (bitmapinfoheader ));
23 bi-> bisize = sizeof (bitmapinfoheader );
24 bi-> biwidth = width;
25 bi-> biheight = height;
26 bi-> biplanes = 1;
27 bi-> bibitcount = deep; // Bm. bmplanes * BM. bmbitspixel;
28 bi-> bicompression = bi_rgb;
29 bi-> bisizeimage = 0;
30 bi-> bixpelspermeter = 0;
31 bi-> biypelspermeter = 0;
32 bi-> biclrused = 0;
33 bi-> biclrimportant = 0;
34}
35
36 bool getimagedata2 (hwnd target, rect, void * data, int * Len) {// obtain bitmap information in hwnd
37 HDC entiredc;
38 rect RC;
39 hbitmap bitmap;
40 HDC compdc;
41 entiredc = getdc (target );
42 // getwindowrect (target, & rc );
43 rc = rect;
44
45 if (RC. right-rc.left <= 0 | RC. bottom-rc.top <= 0 ){
46 Return false;
47}
48 bitmap = createcompatiblebitmap (entiredc, RC. right-rc.left, RC. bottom-rc.top );
49 If (Bitmap = NULL ){
50 releasedc (target, entiredc );
51 return false;
52}
53 compdc = createcompatibledc (entiredc );
54 SelectObject (compdc, bitmap );
55 bitblt (compdc, RC. right-rc.left, RC. bottom-rc.top, entiredc, rect. Left, rect. Top, srccopy );
56
57 bitmapinfo BMI;
58 bitmapinfoheader * Bi;
59 int rgb_len;
60 int lines;
61 Bi = & BMI. bmiheader;
62 char * rgb_buff;
63 fillbitmapinfohdr (Bi, RC. right-rc.left, RC. bottom-rc.top, 24 );
64
65 rgb_len = Bi-> biwidth * bi-> biheight * 3;
66
67 rgb_buff = (char *) data; // new char [rgb_len];
68 if (rgb_buff = NULL ){
69 return false;
70}
71 lines = getdibits (compdc, bitmap, 0, RC. bottom-rc.top, rgb_buff,
72 (lpbitmapinfo) Bi, dib_rgb_colors );
73 deleteobject (Bitmap );
74 releasedc (target, compdc );
75 releasedc (target, entiredc );
76 deletedc (compdc );
77 If (lines = NULL ){
78 // Delete [] rgb_buff;
79 return false;
80}
81 // * Data = rgb_buff;
82 * Len = rgb_len;
83 return true;
84}
85
86
87 bool getimagedata (hwnd target, rect, void ** data, int * Len) {// obtain bitmap information in hwnd
88 HDC entiredc;
89 rect RC;
90 hbitmap bitmap;
91 HDC compdc;
92 entiredc = getdc (target );
93 // getwindowrect (target, & rc );
94 rc = rect;
95 bitmap = createcompatiblebitmap (entiredc, RC. right-rc.left, RC. bottom-rc.top );
96 compdc = createcompatibledc (entiredc );
97 SelectObject (compdc, bitmap );
98 bitblt (compdc, RC. right-rc.left, RC. bottom-rc.top, entiredc, srccopy );
99
100 bitmapinfo BMI;
101 bitmapinfoheader * Bi;
102 int rgb_len;
103 int lines;
104 Bi = & BMI. bmiheader;
105 char * rgb_buff;
106 fillbitmapinfohdr (Bi, RC. right-rc.left, RC. bottom-rc.top, 24 );
107
108 rgb_len = Bi-> biwidth * bi-> biheight * 3;
109 rgb_buff = new char [rgb_len];
110 if (rgb_buff = NULL ){
111 return false;
112}
113 lines = getdibits (compdc, bitmap, 0, RC. bottom-rc.top, rgb_buff,
114 (lpbitmapinfo) Bi, dib_rgb_colors );
115 deleteobject (Bitmap );
116 releasedc (target, compdc );
117 releasedc (target, entiredc );
118 deletedc (compdc );
119 If (lines = NULL ){
120 Delete [] rgb_buff;
121 return false;
122}
123 * Data = rgb_buff;
124 * Len = rgb_len;
125 return true;
126}
127
128
129
130 void captureimage (hwnd target, const char * destfile) {// obtain the bitmap of hwnd as a file
131 rect RC;
132 getwindowrect (target, & rc );
133 void * data;
134 int Len;
135 getimagedata (target, RC, & Data, & Len );
136
137 bitmapinfo BMI;
138 bitmapinfoheader * Bi;
139 Bi = & BMI. bmiheader;
140 fillbitmapinfohdr (Bi, RC. right-rc.left, RC. bottom-rc.top, 24 );
141
142 saveimage (destfile, Data, Len, Bi );
143
144 Delete [] data;
145}
146
147 void drawbitmap2 (hwnd, rect, char * data, int deep) // draw bitmap in hwnd
148 {
149 bitmapinfoheader * Bi;
150 bitmapinfo: bitmap;
151 Bi = & bitmap. bmiheader;
152 memset (& bitmap, 0, sizeof (Bitmap ));
153 bi-> bisize = sizeof (bitmapinfoheader );
154 biwidth = rect. Right-rect. Left;
155 bi-> biheight = rect. Bottom-rect. Top;
156 bi-> biplanes = 1;
157 bi-> bibitcount = deep; // Bm. bmplanes * BM. bmbitspixel;
158 bi-> bicompression = bi_rgb;
159 bi-> bisizeimage = Bi-> biwidth * bi-> biheight * Deep/3;
160 bi-> bixpelspermeter = 0;
161 bi-> biypelspermeter = 0;
162 bi-> biclrused = 0;
163 bi-> biclrimportant = 0;
164
165 HDC = getdc (hwnd );
166
167 stretchdibits (HDC,
168 rect. Left,
169 rect. Top,
170 rect. right-rect.left,
171 rect. Bottom-rect. Top,
172 // RC. Right-1,
173 // RC. Bottom-1,
174 0,
175 0,
176 rect. right-rect.left,
177 rect. Bottom-rect. Top,
178 (const void *) data,
179 & bitmap,
180 dib_rgb_colors,
181 srccopy );
182
183 // releasedc (hwnd, HDC );
184 releasedc (hwnd, HDC );
185}
186
187 void stretchdrawbitmap (hwnd, int width, int height, char * data, int deep) // draw bitmap in hwnd
188 {
189 bitmapinfoheader * Bi;
190 bitmapinfo: bitmap;
191 Bi = & bitmap. bmiheader;
192 memset (& bitmap, 0, sizeof (Bitmap ));
193 bi-> bisize = sizeof (bitmapinfoheader );
194 bi-> biwidth = width;
195 bi-> biheight = height;
196 bi-> biplanes = 1;
197 bi-> bibitcount = deep; // Bm. bmplanes * BM. bmbitspixel;
198 bi-> bicompression = bi_rgb;
199 bi-> bisizeimage = Bi-> biwidth * bi-> biheight * Deep/3;
200 bi-> bixpelspermeter = 0;
201 bi-> biypelspermeter = 0;
202 bi-> biclrused = 0;
203 bi-> biclrimportant = 0;
204 rect RC;
205 HDC = getdc (hwnd );
206: getwindowrect (hwnd, & rc );
207 stretchdibits (HDC,
208 0,
209 0,
210 RC. Right-RC. Left,
211 RC. Bottom-RC. Top,
212 0,
213 0,
214 width,
215 height,
216 (const void *) data,
217 & bitmap,
218 dib_rgb_colors,
219 srccopy );
220
221 // releasedc (hwnd, HDC );
222 releasedc (hwnd, HDC );
223}
224
225 void drawbitmap (hwnd, int width, int height, char * data, int deep) // draw bitmap in hwnd
226 {
227 bitmapinfoheader * Bi;
228 bitmapinfo: bitmap;
229 Bi = & bitmap. bmiheader;
230 memset (& bitmap, 0, sizeof (Bitmap ));
231 bi-> bisize = sizeof (bitmapinfoheader );
232 Bi-> biwidth = width;
233 bi-> biheight = height;
234 bi-> biplanes = 1;
235 bi-> bibitcount = deep; // Bm. bmplanes * BM. bmbitspixel;
236 bi-> bicompression = bi_rgb;
237 bi-> bisizeimage = width * height * deep;
238 bi-> bixpelspermeter = 0;
239 bi-> biypelspermeter = 0;
240 bi-> biclrused = 0;
241 bi-> biclrimportant = 0;
242
243 HDC = getdc (hwnd );
244
245 stretchdibits (HDC,
246 0,
247 0,
248 width-1,
249 height-1,
250 // RC. Right-1,
251 // RC. Bottom-1,
252 0,
253 0,
254 width,
255 height,
256 (const void *) data,
257 & bitmap,
258 dib_rgb_colors,
259 srccopy );
260
261 // releasedc (hwnd, HDC );
262 releasedc (hwnd, HDC );
263}