forked from gaojunxin/TSPlug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSFindPicture.cpp
6456 lines (5869 loc) · 155 KB
/
TSFindPicture.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
本源码由TC简单软件科技有限公司开源,功能可以自由修改、发布、
长沙简单软件科技有限公司对于源码不做后期维护,,请大家在使用过程中遵循开源协议
*/
#include "stdafx.h"
#include "TSRuntime.h"
#include "TSFindPicture.h"
#include "D3dx9tex.h"
#include "DXBind.h"
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
extern HWND g_currentHwnd;
MyFindPictureClass::MyFindPictureClass(void)
{
IsLastFindStr=false;
m_displayMode=0;
m_hwnd=NULL;
hDXBitmap=NULL;
pLoadBmpBuffer=NULL;
pWndBmpBuffer=NULL;
pBuffer=NULL;
pbuffer=NULL;
loadWidth=0;
loadHeight=0;
wndWidth=0;
wndHeight=0;
nbuffersize=0;
m_helpXpoint = 0;
m_helpYpoint = 0;
m_parenthwnd=0;
memset(SetPath,0,MAX_PATH);
strColor=0;
//strColoroff=0;
usingindex=0;
for(int i=0;i<MAX_PATH*10;i++)
memset(addrxy[i],0,3);
memset(strColors,0,MAX_PATH);
memset(strColoroff,0,MAX_PATH);
memset(m_colorOffR,0,MAX_PATH);
memset(m_colorOffG,0,MAX_PATH);
memset(m_colorOffB,0,MAX_PATH);
//memset(pWndBmpBuffer,0,sizeof(pWndBmpBuffer));
//memset(SetPicPwdString,0,MAX_PATH);
isprocessColor=false;//默认不是找色或者取色
dictindex=0;
}
MyFindPictureClass::~MyFindPictureClass(void)
{
}
int MyFindPictureClass::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
// Initialize GDI+.(初始化GDI,否则调用GDI函数会导致异常)
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Initialize GDI+.(初始化GDI,否则调用GDI函数会导致异常)
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
{
return -1; // Failure
}
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
bool MyFindPictureClass::BMP2PNG(wchar_t* strBMPFile,wchar_t* strPNGFile)
{
CLSID encoderClsid;
WCHAR strGuid[39];
if(GetEncoderClsid(L"image/png", &encoderClsid) >= 0)
StringFromGUID2(encoderClsid, strGuid, 39);
Image image(strBMPFile);
image.Save(strPNGFile,&encoderClsid,NULL);
return true;
}
bool MyFindPictureClass::BMP2JPG(wchar_t* strBMPFile,wchar_t* strJPGFile)
{
CLSID encoderClsid;
WCHAR strGuid[39];
if(GetEncoderClsid(L"image/jpeg", &encoderClsid) >= 0)
StringFromGUID2(encoderClsid, strGuid, 39);
Image image(strBMPFile);
image.Save(strJPGFile,&encoderClsid,NULL);
//关闭GDI
//GdiplusShutdown(gdiplusToken);
return true;
}
bool MyFindPictureClass::loadBitmap(wchar_t* path)
{
//std::string rstr;
BYTE *rstr=NULL;
int rstrlen=0;
BITMAPFILEHEADER fileheader={0};
HANDLE file_Handle = CreateFile(path,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(file_Handle==INVALID_HANDLE_VALUE)
{
int err=::GetLastError();
if(TSRuntime::IsShowErrorMsg)
{
char erro[MAX_PATH]={0};
USES_CONVERSION;
sprintf(erro,"加载:%s,位图失败",W2A(path));
::MessageBoxA(NULL,erro,"TS",0);
}
CloseHandle(file_Handle);
return false;
}
DWORD dword=0,ReadFileSize=0;
DWORD fileszie=::GetFileSize(file_Handle,NULL);
rstr=new BYTE[fileszie+1];
memset(rstr,0,sizeof(BYTE)*fileszie);
{
::ReadFile(file_Handle,rstr,fileszie,&ReadFileSize,NULL);
}
CloseHandle(file_Handle);
BITMAPINFOHEADER head;
DWORD headSize=sizeof(BITMAPINFOHEADER);
memcpy(&head,&rstr[sizeof(BITMAPFILEHEADER)],sizeof(BITMAPINFOHEADER));
loadWidth = head.biWidth;
loadHeight = head.biHeight;
WORD biBitCount = head.biBitCount;
if(biBitCount != 24)
{
if(TSRuntime::IsShowErrorMsg)
::MessageBoxA(NULL,"请选择24位位图!","TS",0);
return false;
}
int totalSize = (loadWidth *biBitCount/8+3)/4*4*loadHeight;
//BYTE *pBmpBuf = new BYTE[totalSize];
//memset(pBmpBuf,0,sizeof(BYTE)*totalSize);
size_t size = 0;
size=rstrlen-headSize-sizeof(BITMAPFILEHEADER);
BYTE *pBmpBuf =&rstr[headSize+sizeof(BITMAPFILEHEADER)];
//memcpy(pBmpBuf,&rstr[headSize+sizeof(BITMAPFILEHEADER)],size);
pLoadBmpBuffer = new COLORREF*[loadHeight]; ///// 二维数组 用来存储图像的颜色值
for(int i=0;i<loadHeight;i++)
{
pLoadBmpBuffer[i]=new COLORREF[loadWidth];
}
COLORREF helpcolor=0;
int pitch=loadWidth%4;
//HDC hDC = ::GetWindowDC(::GetDesktopWindow());
for(int i=0;i<loadHeight;i++)
{
int realPitch=i*pitch;
for(int j=0;j<loadWidth;j++)
{
UCHAR b=pBmpBuf[(i*loadWidth+j)*3+realPitch];
UCHAR g=pBmpBuf[(i*loadWidth+j)*3+1+realPitch];
UCHAR r=pBmpBuf[(i*loadWidth+j)*3+2+realPitch];
helpcolor=RGB(r,g,b);
pLoadBmpBuffer[loadHeight-i-1][j]=helpcolor;
}
}
delete [] rstr;
return true;
}
//
//bool MyFindPictureClass::loadBitmap(wchar_t* path)
// {
// //TSRuntime::add_log( "loadBitmap");
// ///////////////////////////加载的位图 //////////////////////////
// HBITMAP hbmp = (HBITMAP)LoadImage(NULL,path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE );
// if(hbmp==NULL)
// {
// //::MessageBox(NULL,L"加载位图失败",path,0);
// return false;
// }
//
// BITMAP bitmap;
// GetObject(hbmp, sizeof(BITMAP), &bitmap);
//
// BITMAPINFO info;
// HDC dc;
// dc=::CreateDC(L"DISPLAY",NULL,NULL,NULL);
//
// // 24位图的BITMAPINFO
// BITMAPINFO *pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
// memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
// BITMAPINFOHEADER *pInfo_Header = (BITMAPINFOHEADER *)pBITMAPINFO;
// pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
// pInfo_Header->biWidth =bitmap.bmWidth;
// pInfo_Header->biHeight = bitmap.bmHeight;
// pInfo_Header->biPlanes = 1;
// pInfo_Header->biBitCount = 24;
// pInfo_Header->biCompression = BI_RGB;
//
// long width = bitmap.bmWidth;
// long height = bitmap.bmHeight;
// info.bmiHeader.biBitCount=24;
//
// loadWidth = width;
// loadHeight = height;
//
// DWORD bufSize = ( width * 3 + 3) / 4 * 4 * height;
// BYTE *buffer=new BYTE[bufSize];
// memset(buffer,0,sizeof(BYTE)*bufSize);
// if(!GetDIBits(dc, hbmp, 0, bitmap.bmHeight, buffer, pBITMAPINFO, DIB_RGB_COLORS))
// {
// if(TSRuntime::IsShowErrorMsg)
// ::MessageBox(NULL,L"加载位图失败",L"TC",0);
// return false;
// }
// pLoadBmpBuffer = new COLORREF*[height]; ///// 二维数组 用来存储图像的颜色值
// for(int i=0;i<height;i++)
// {
// pLoadBmpBuffer[i]=new COLORREF[width];
// }
// COLORREF helpcolor=0;
// int pitch=width%4;
// HDC hDC = ::GetWindowDC(::GetDesktopWindow());
// for(int i=0;i<height;i++)
// {
// int realPitch=i*pitch;
// for(int j=0;j<width;j++)
// {
// UCHAR b=buffer[(i*width+j)*3+realPitch];
// UCHAR g=buffer[(i*width+j)*3+1+realPitch];
// UCHAR r=buffer[(i*width+j)*3+2+realPitch];
// helpcolor=RGB(r,g,b);
// SetPixel(hDC,j,i,helpcolor);
// pLoadBmpBuffer[height-i-1][j]=helpcolor;
// }
// }
// ///////////////////////////获得加载位图 颜色成功 //////////////////////////
// delete [] buffer;
// ::DeleteDC(dc);
// ::free(pBITMAPINFO);
// ::DeleteObject(hbmp);
// return true;
// }
bool MyFindPictureClass::SaveGDIBitmap(HWND hWnd,RECT rect,wchar_t *savepath)
{
HDC hDC;
//hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
hDC=GetDCEx(hWnd,NULL,DCX_PARENTCLIP );
HDC hMemDC; //内存缓冲设备环境
HBITMAP hbmMem,hbmOld; //内存缓冲设备环境中的位图
RECT rc=rect;
//判断边境值
RECT clientrc;
::GetClientRect(hWnd,&clientrc);
int xc =0;
int cx =0;
int cy =0;
if(rc.bottom>clientrc.bottom)
rc.bottom=clientrc.bottom;
if(rc.right>clientrc.right)
rc.right=clientrc.right;
//TSRuntime::add_log( "clientrc.right:%d,clientrc.bottom:%d,rect.left:%d,rect.top:%d,rect.right:%d,rect.bottom:%d",clientrc.right, clientrc.bottom,rc.left,rc.top,rc.right,rc.bottom);
// 24位图的BITMAPINFO
BITMAPINFO *pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
BITMAPINFOHEADER *pInfo_Header = (BITMAPINFOHEADER *)pBITMAPINFO;
pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
pInfo_Header->biWidth = rc.right - rc.left;
pInfo_Header->biHeight = (rc.bottom - rc.top);
pInfo_Header->biPlanes = 1;
pInfo_Header->biBitCount = 24;
pInfo_Header->biCompression = BI_RGB;
hMemDC=CreateCompatibleDC(hDC); //创建内存兼容设备环境
//创建内存兼容位图
hbmMem=CreateCompatibleBitmap(hDC,pInfo_Header->biWidth,pInfo_Header->biHeight);
hbmOld=(HBITMAP)SelectObject(hMemDC,hbmMem);
if((m_displayMode&DISPLAY_GDI2)==DISPLAY_GDI2)
{
RECT winret;
::GetWindowRect(hWnd,&winret);
POINT point;
point.x=0;
point.y=0;
::ClientToScreen(hWnd,&point);
cx=point.x-winret.left;
cy=point.y-winret.top;
HDC T_hMemDC=CreateCompatibleDC(hDC); //创建内存兼容设备环境
//创建内存兼容位图
HBITMAP T_hbmMem=CreateCompatibleBitmap(hDC,pInfo_Header->biWidth+cx+rc.left,pInfo_Header->biHeight+cy+rc.top);
HBITMAP T_hbmOld=(HBITMAP)SelectObject(T_hMemDC,T_hbmMem);
::UpdateWindow(hWnd);
::RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN|RDW_FRAME);
::PrintWindow(hWnd,T_hMemDC,0);
BitBlt(hMemDC,0,0,pInfo_Header->biWidth,pInfo_Header->biHeight,T_hMemDC,cx+rc.left,xc+cy+rc.top,CAPTUREBLT|SRCCOPY);
DeleteDC(T_hMemDC);
DeleteObject(T_hbmMem);
DeleteObject(T_hbmOld);
}
else
{
//将内存设备环境中的内容绘制到物理设备环境 hDC
BitBlt(hMemDC,0,0,pInfo_Header->biWidth,pInfo_Header->biHeight,hDC,cx+rc.left,xc+cy+rc.top,CAPTUREBLT|SRCCOPY);
}
HBITMAP hBitmap=(HBITMAP)SelectObject(hMemDC,hbmOld);
// 获得数据buf
DWORD bufSize=(pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
BYTE * pBuffer = new BYTE[bufSize];
int aHeight=pInfo_Header->biHeight;
if(::GetDIBits(hMemDC, hBitmap, 0, aHeight, pBuffer,pBITMAPINFO, DIB_RGB_COLORS) == 0)
{
if(TSRuntime::IsShowErrorMsg)
::MessageBox(NULL,L"加载位图失败",L"TS",0);
return false;
}
bool bret=SaveBitmapToFile(hBitmap,savepath);
ReleaseDC(hWnd,hDC);
//释放资源
DeleteObject(hbmMem);
DeleteObject(hbmOld);
DeleteDC(hMemDC);
free(pBITMAPINFO);
::DeleteObject(hBitmap);
delete [] pBuffer;
return bret;
}
bool SaveDXBitmapToFile(PVOID pBuffer,DWORD nbuffersize,LPCWSTR lpFileName)
{
//////////32位BMP转换24位BMP 2012-8-14
BYTE *pTemp =(BYTE *) pBuffer; //已知 32位 数据
BYTE *pData24,*tpData24;
DWORD dwSize32=nbuffersize; //已知,32位大小
DWORD dwSize24;
dwSize24 = (dwSize32*3)/4;
pData24 = new BYTE[dwSize24];
tpData24 =pData24;
for(int index=0;index<dwSize32/4;index++) //像素总个数
{
unsigned char r = *(pTemp++);
unsigned char g = *(pTemp++);
unsigned char b= *(pTemp++);
(pTemp++); //去掉alpha
*(pData24++) = r;
*(pData24++) = g;
*(pData24++) = b;
}
return true;
}
bool MyFindPictureClass::CopyScreenToBitmap(LPRECT lpRect,wchar_t *savepath)
{
HDC hScrDC, hMemDC;
// 屏幕和内存设备描述表
HBITMAP hBitmap,hOldBitmap;
// 位图句柄
int nX, nY, nX2, nY2;
// 选定区域坐标
int nWidth, nHeight;
// 位图宽度和高度
int xScrn, yScrn;
// 屏幕分辨率
// 确保选定区域不为空矩形
if (IsRectEmpty(lpRect))
return NULL;
//为屏幕创建设备描述表
hScrDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 获得选定区域坐标
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
//nX = 10;
//nY = 10;
//nX2 = 500;
//nY2 = 600;
// 获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//确保选定区域是可见的
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
// 把新位图选到内存设备描述表中
hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC,0,0, nWidth,nHeight,hScrDC, nX, nY, CAPTUREBLT|SRCCOPY);
//得到屏幕位图的句柄
hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);
bool bret=SaveBitmapToFile(hBitmap,savepath);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
DeleteObject(hOldBitmap);
DeleteObject(hBitmap);
// 返回位图句柄
return bret;
}
//LPSTR lpFileName
bool MyFindPictureClass::SaveBitmapToFile(HBITMAP hBitmap, LPCWSTR lpFileName)
{
HDC hDC;
//设备描述表
int iBits;
//当前显示分辨率下每个像素所占字节数
WORD wBitCount;
//位图中每个像素所占字节数
//定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap;
//位图属性结构
BITMAPFILEHEADER bmfHdr;
//位图文件头结构
BITMAPINFOHEADER bi;
//位图信息头结构
LPBITMAPINFOHEADER lpbi;
//指向位图信息头结构
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL;
//定义文件,分配内存句柄,调色板句柄
//计算位图文件每个像素所占字节数
hDC = CreateDC(L"DISPLAY",NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
//wBitCount = 32;
wBitCount = 24;
//计算调色板大小
if (wBitCount <= 8)
dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);
//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
//为位图内容分配内存
/*xxxxxxxx计算位图大小分解一下(解释一下上面的语句)xxxxxxxxxxxxxxxxxxxx
//每个扫描行所占的字节数应该为4的整数倍,具体算法为:
int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
if((Bitmap.bmWidth*wBitCount) % 32)
biWidth++; //不是整数倍的加1
biWidth *= 4;//到这里,计算得到的为每个扫描行的字节数。
dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
//TSRuntime::add_log("wBitCount:%d",wBitCount);
hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
// 处理调色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
RealizePalette(hDC);
}
// 获取该调色板下新的像素值
GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
//恢复调色板
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//创建位图文件
fh=CreateFile(lpFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh==INVALID_HANDLE_VALUE)
return FALSE;
// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
bool bw1=false;
bool bw2=false;
// 写入位图文件头
bw1=WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 写入位图文件其余内容
bw2=WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
//return TRUE;
if(bw1&&bw2==true)
{
return true;
}
else
{
return false;
}
}
bool MyFindPictureClass::getGDIBitmap(HWND hWnd,RECT rc,int typemode)
{
//TSRuntime::add_log( "getGDIBitmap:hWnd:%d",hWnd);
//HDC hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
m_hwnd=hWnd;
HDC hDC;
if(hWnd!=GetDesktopWindow())
//hDC=::GetWindowDC(hWnd); //定义绘图用的设备环境
hDC=GetDCEx(hWnd,NULL,DCX_PARENTCLIP );
else
hDC=CreateDC(L"DISPLAY", NULL, NULL, NULL);
HDC hMemDC; //内存缓冲设备环境
HBITMAP hbmMem,hbmOld; //内存缓冲设备环境中的位图
int xc =0;
int cx =0;
int cy =0;
wndWidth=m_Right-m_Left;
wndHeight=m_bottom-m_Top;
if(wndWidth>=2000||wndHeight>=2000)//大于边界值就返回错误,以防坐标越界
return false;
BITMAPINFO pBITMAPINFO;
pBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBITMAPINFO.bmiHeader.biWidth = wndWidth;
pBITMAPINFO.bmiHeader.biHeight = wndHeight;
pBITMAPINFO.bmiHeader.biPlanes = 1;
pBITMAPINFO.bmiHeader.biBitCount = 24;
pBITMAPINFO.bmiHeader.biCompression = BI_RGB;
hMemDC=CreateCompatibleDC(hDC); //创建内存兼容设备环境
//创建内存兼容位图
hbmMem=CreateCompatibleBitmap(hDC,wndWidth,wndHeight);
hbmOld=(HBITMAP)SelectObject(hMemDC,hbmMem);
if((m_displayMode&DISPLAY_GDI2)==DISPLAY_GDI2)
{
RECT winret;
::GetWindowRect(hWnd,&winret);
POINT point;
point.x=0;
point.y=0;
::ClientToScreen(hWnd,&point);
cx=point.x-winret.left;
cy=point.y-winret.top;
HDC T_hMemDC=CreateCompatibleDC(hDC); //创建内存兼容设备环境
//创建内存兼容位图
HBITMAP T_hbmMem=CreateCompatibleBitmap(hDC,wndWidth+cx+rc.left,wndHeight+cy+rc.top);
HBITMAP T_hbmOld=(HBITMAP)SelectObject(T_hMemDC,T_hbmMem);
::UpdateWindow(hWnd);
::RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN|RDW_FRAME);
::PrintWindow(hWnd,T_hMemDC,0);
BitBlt(hMemDC,0,0,wndWidth,wndHeight,T_hMemDC,cx+rc.left,xc+cy+rc.top,CAPTUREBLT|SRCCOPY);
DeleteDC(T_hMemDC);
DeleteObject(T_hbmMem);
DeleteObject(T_hbmOld);
}
else
{
//将内存设备环境中的内容绘制到物理设备环境 hDC
BitBlt(hMemDC,0,0,wndWidth,wndHeight,hDC,cx+m_Left,xc+cy+m_Top,CAPTUREBLT|SRCCOPY);
}
HBITMAP hBitmap=(HBITMAP)SelectObject(hMemDC,hbmOld);
// 获得数据buf
//DWORD bufSize=(pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
//BYTE * pBuffer = new BYTE[bufSize];
int aHeight=wndHeight;
if(::GetDIBits(hMemDC, hBitmap, 0, aHeight, pBuffer,&pBITMAPINFO, DIB_RGB_COLORS) == 0)
{
if(TSRuntime::IsShowErrorMsg)
::MessageBox(NULL,L"加载位图失败",L"TS",0);
return false;
}
//HDC dd = ::GetWindowDC(::GetDesktopWindow());
COLORREF helpcolor=0;
int pitch=wndWidth%4;
if(typemode==1)//找字
{
for(int i=0;i<wndHeight;i++)
{
int realPitch=i*pitch;
for(int j=0;j<wndWidth;j++)
{
UCHAR b=pBuffer[(i*wndWidth+j)*3+realPitch];
UCHAR g=pBuffer[(i*wndWidth+j)*3+1+realPitch];
UCHAR r=pBuffer[(i*wndWidth+j)*3+2+realPitch];
//helpcolor=RGB(r,g,b);
bool dwfind=false;
for(int n=0;n<ncolors;n++)
{
BYTE colorR = GetRValue(strColors[n]);
BYTE colorG = GetGValue(strColors[n]);
BYTE colorB = GetBValue(strColors[n]);
//处理色偏,把图二值化
////处理色偏
if(abs(colorR-r)<=m_colorOffR[n]&&abs(colorG-g)<=m_colorOffG[n]&&abs(colorB-b)<=m_colorOffB[n])
{
helpcolor=RGB(255,255,255);
dwfind=true;
}
else
{
helpcolor=RGB(0,0,0);
}
//SetPixel(dd,j,(wndHeight-i-1),helpcolor);
pWndBmpBuffer[wndHeight-i-1][j]=helpcolor;
if(dwfind)
break;
}
}
}
}
else //找图
{
for(int i=0;i<wndHeight;i++)
{
int realPitch=i*pitch;
for(int j=0;j<wndWidth;j++)
{
UCHAR b=pBuffer[(i*wndWidth+j)*3+realPitch];
UCHAR g=pBuffer[(i*wndWidth+j)*3+1+realPitch];
UCHAR r=pBuffer[(i*wndWidth+j)*3+2+realPitch];
helpcolor=RGB(r,g,b);
pWndBmpBuffer[wndHeight-i-1+m_Top][j+m_Left]=helpcolor;
//::SetPixel(dd,j,i,helpcolor);
}
}
}
if(hWnd==GetDesktopWindow())
DeleteDC(hDC);
else
ReleaseDC(hWnd,hDC);
//释放资源
DeleteObject(hbmMem);
DeleteObject(hbmOld);
DeleteDC(hMemDC);
DeleteDC(hDC);
//free(pBITMAPINFO);
::DeleteObject(hBitmap);
//delete [] pBuffer;
return true;
}
bool MyFindPictureClass::getDXBitmap(PVOID pBmp,DWORD (*ColorDataInfo)[2000],int typemode)
{
if(wndWidth>=2000||wndHeight>=2000)//大于边界值就返回错误,以防坐标越界
return false;
//HDC dd = ::GetWindowDC(::GetDesktopWindow());
//TSRuntime::add_log( "wndHeight:%d wndWidth:%d,m_left:%d,m_top:%d,m_Right:%d,m_bottom:%d",wndHeight,wndWidth,m_Left,m_Top,m_Right,m_bottom);
COLORREF helpcolor=0;
//int pitch=wndWidth%4;
if(typemode==1)//找字
{
for(int i=0;i<wndHeight;i++)
{
//int realPitch=i*pitch;
for(int j=0;j<wndWidth;j++)
{
UCHAR r=GetRValue(ColorDataInfo[i][j]);
UCHAR g=GetGValue(ColorDataInfo[i][j]);
UCHAR b=GetBValue(ColorDataInfo[i][j]);
bool dwfind=false;
for(int n=0;n<ncolors;n++)
{
BYTE colorR = GetRValue(strColors[n]);
BYTE colorG = GetGValue(strColors[n]);
BYTE colorB = GetBValue(strColors[n]);
//处理色偏,把图二值化
if(abs(colorR-r)<=m_colorOffR[n]&&abs(colorG-g)<=m_colorOffG[n]&&abs(colorB-b)<=m_colorOffB[n])
{
helpcolor=RGB(255,255,255);
dwfind=true;
}
else
{
helpcolor=RGB(0,0,0);
}
//COLORREF helpcolor1=RGB(r,g,b);
//SetPixel(dd,j,i,helpcolor1);
pWndBmpBuffer[i][j]=helpcolor;
//pWndBmpBuffer[wndHeight-i-1+m_Top][j+m_Left]=helpcolor;
if(dwfind)
break;
}
}
}
}
else //找图
{
for(int i=0;i<wndHeight;i++)
{
for(int j=0;j<wndWidth;j++)
{
//helpcolor=(COLORREF)ColorDataInfo[i][j];
//SetPixel(dd,j,i,helpcolor);
pWndBmpBuffer[m_Top+i][j+m_Left]=(COLORREF)ColorDataInfo[i][j];
}
}
}
return true;
///////////////////////////获取窗口位图的 颜色成功 //////////////////////////
}
//
//
//
int MyFindPictureClass::findPicture(PVOID pbuffer,HWND hwnd,int left, int top, int right, int bottom,
wchar_t* path, wchar_t* color, double simi, int dir,
long &xpos, long &ypos,DWORD (*ColorDataInfo)[2000],wchar_t *retstring,int type)
{
int index = -1;
int retIndex=-1;
bool isFind=false;
if(hwnd!=0)
{
if(::IsIconic(hwnd))
{
//::MessageBox(0,L"超过边界值,GDI模式不支持最小化找图",L"TS",0);
return -1;
}
}
////////处理颜色和色偏
WCHAR colorR[4]={0};//字的颜色
WCHAR colorG[4]={0};//字的颜色
WCHAR colorB[4]={0};//字的颜色
int count=0;
int colorL=0;
while(color[colorL]!=L'\0')
{
if(count>6) //判断是否是非法颜色值
break;
if(count<2)
colorR[count]=color[colorL++];
else if(count>1&&count<4)
colorG[count-2]=color[colorL++];
else if(count>3)
colorB[count-4]=color[colorL++];
count++;
}
//将传入的RGB转换为BGR
WCHAR colorBGR[16]={0};//字的颜色
swprintf(colorBGR,L"%s%s%s",colorB,colorG,colorR);
strColor=wcstol(colorBGR,NULL,16);//整形颜色值
m_colorOffsR = GetRValue(strColor);
m_colorOffsG = GetGValue(strColor);
m_colorOffsB = GetBValue(strColor);
////////处理颜色和色偏
/*m_sim = simi;*/ //未使用该变量
m_simColor = 0;
if(simi==0.5)
m_simColor=49;
else if(simi==0.6)
m_simColor=38;
else if(simi==0.7)
m_simColor=29;
else if(simi==0.8)
m_simColor=18;
else if(simi==0.9)
m_simColor=9;
/////////////////////////////////////
m_Dir = dir;
/////////////////////////////控制四点的边界值以防止越界//////////////////////////
wchar_t pathBuffer[MAX_PATH]={0};
wchar_t RCPath[MAX_PATH]={0};
if(m_parenthwnd!=0&&m_parenthwnd!=::GetDesktopWindow())//normol找图
{
RECT rec;
RECT clientrec;
::GetClientRect(m_parenthwnd,&clientrec);
int clienthight=clientrec.bottom-clientrec.top;
int clientwide=clientrec.right-clientrec.left;
::GetWindowRect(m_parenthwnd,&rec);
POINT point;
point.x=0;
point.y=0;
::ClientToScreen(m_parenthwnd,&point);
m_Left=point.x;
m_Top=point.y;
m_Right=rec.right;
m_bottom=rec.bottom;
if(left<0)
left=0;
if(left >= clientwide)
left = clientwide-1;
if(top<0)
top=0;
if(top >= clienthight)
top = clienthight-1;
if(right >= clientwide)
right=clientwide-1;
if(bottom >= clienthight)
bottom=clienthight-1;
if(m_Left<0)
{
if((left+m_Left)<=0)//越界
left=0;
else
left=m_Left+left;
}
else
left=m_Left+left;
if(m_Top>=0)
top=m_Top+top;
if(m_Right >= ::GetSystemMetrics(SM_CXSCREEN))
{
if((right+m_Left)>::GetSystemMetrics(SM_CXSCREEN))
right=::GetSystemMetrics(SM_CXSCREEN)-1;
else
right=right+m_Left;
}
else
right=right+m_Left;
if(m_bottom >= ::GetSystemMetrics(SM_CYSCREEN))
{
if((bottom+m_Top)>=::GetSystemMetrics(SM_CYSCREEN))
bottom=::GetSystemMetrics(SM_CYSCREEN)-1;
else
bottom=bottom+m_Top;
}
else
bottom=bottom+m_Top;
m_Left = left;
m_Top = top;
m_Right = right;
m_bottom = bottom;
}
else
{
RECT clientrec;
if(ColorDataInfo!=NULL)//DX
::GetClientRect(m_hwnd,&clientrec);
else//gdi
::GetClientRect(hwnd,&clientrec);
int clienthight=clientrec.bottom-clientrec.top;
int clientwide=clientrec.right-clientrec.left;
m_Left = left;
m_Top = top;
m_Right = right;
m_bottom = bottom;
//TSRuntime::add_log( "clientwide:%d,clienthight:%d,m_Left:%d,m_Top:%d,m_Right:%d,m_bottom:%d",clientwide,clienthight,left,top,right,bottom);
if(left<0)
m_Left = left =0;
if(left >= clientwide)
m_Left = clientwide-1;
if(top<0)
m_Top = top =0;
if(top >= clienthight)
m_Top = clienthight-1;
if(right >= clientwide)
m_Right=clientwide-1;
if(bottom >= clienthight)
m_bottom=clienthight-1;
//TSRuntime::add_log( "m_Left:%d,m_Top:%d,m_Right:%d,m_bottom:%d",m_Left,m_Top,m_Right,m_bottom);
}
RECT rc;
rc.bottom=m_bottom;
rc.left=m_Left;
rc.right=m_Right;
rc.top=m_Top;
if(ColorDataInfo!=NULL)
{
wndWidth=m_Right-m_Left;
wndHeight=m_bottom-m_Top;
//DX模式找字
if(!getDXBitmap(pbuffer,ColorDataInfo))
{
return retIndex;
}
}
else