(原创)在指定区域显示图片

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
int ShowPIC(CDC *pDC, CString strPath, CRect rect, int ShowType)
{
CString lastchar;
lastchar = "";
lastchar = strPath.Right(3);
if(lastchar == "ico" || lastchar == "ICO")
{
HICON myicon;
myicon = (HICON)LoadImage(NULL, strPath,
IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_VGACOLOR);
DrawIconEx(pDC->GetSafeHdc(),rect.left,rect.top,myicon,rect.Width(),rect.Height(),0,NULL,DI_NORMAL);
DestroyIcon(myicon);
m_PolyAddPara.imageHeight = 32;
m_PolyAddPara.imageWidth = 32;
}
else if(lastchar == "exe" || lastchar == "EXE")
{
HICON myicon;
myicon = ::ExtractIcon(AfxGetInstanceHandle(),strPath,0);
DrawIconEx(pDC->GetSafeHdc(),rect.left,rect.top,myicon,rect.Width(),rect.Height(),0,NULL,DI_NORMAL);
DestroyIcon(myicon);
m_PolyAddPara.imageHeight = 32;
m_PolyAddPara.imageWidth = 32;
}
else//显示BMP JPG GIF等格式的图片
{
IStream *pStm = NULL;//2017.08.18
CFileStatus fstatus;
CFile file;
LONG cb;
HGLOBAL hGlobal = NULL;//2017.08.18
//打开文件并检测文件的有效性
//if (!file.Open(strPath,CFile::modeRead))
// return -4;

// if(!file.GetStatus(strPath,fstatus))
// return -5;

//if((cb = fstatus.m_size) == -1)
// return -6;
if (file.Open(strPath,CFile::modeRead)&&
file.GetStatus(strPath,fstatus)&&
((cb = fstatus.m_size) != -1))
{
hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
pvData = GlobalLock(hGlobal);
if (pvData != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
}
}
else
{
if(file)
file.Close();
return -1;
}
//打开文件结束
//显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
//要显示动画GIF请使用ACTIVE控件。
if(hGlobal == NULL || pStm == NULL)
{
if(hGlobal != NULL)
{
//释放内存
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
}
if(pStm != NULL)
{
pStm->Release();
}
file.Close();
return -10;
}

IPicture *pPic = NULL;//F20170818
if(SUCCEEDED(OleLoadPicture(pStm, fstatus.m_size, TRUE, IID_IPicture, (LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
double fX,fY;
//得到图片的高度与宽度
fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/
((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/
((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);

m_PolyAddPara.imageHeight = (long)fY;
m_PolyAddPara.imageWidth = (long)fX;

long nwidth,nheight,nleft,ntop;
int Rx,Ry;
Rx = rect.Width();
Ry = rect.Height();
nheight = Ry;
nwidth = Rx;
nleft = rect.left;
ntop = rect.top;
//检测显示模式
//是否拉伸充满区域显示
if(ShowType == 1)
{
nheight = Ry;
nwidth = Rx;
nleft = rect.left;
ntop = rect.top;
}
//是否居中且按正常比例显示
else if(ShowType == 2)
{
if(fX < Rx)
nwidth = (long)fX;
if(fY < Ry)
nheight = (long)fY;

if((fY > Ry) || (fX > Rx))
{
double temp,temp2;
temp = Ry / fY;
temp2 = Rx / fX;
if(temp < temp2)
{
nwidth = (long)(fX * temp);
nleft = (Rx-nwidth)/2 + rect.left;
}
else if(temp >= temp2)
{
nheight = (long)(fY * temp2);
ntop = (Ry - nheight)/2 + rect.top;
}
}
}
//用 Render函数显示图片
if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0,
hmHeight,hmWidth,-hmHeight,NULL)))
{
//释放内存
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
pPic->Release();
pStm->Release();
file.Close();
return -2;
}

pPic->Release(); //2008.01.31
pStm->Release();
file.Close();
}
else
{
//释放内存
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
pStm->Release();
file.Close();
return -3;
}
//释放内存
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
}
return 1;
}
多谢打赏
-------------本文结束感谢您的阅读-------------
0%