forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGdiPlusBitmap.h
186 lines (155 loc) · 4.02 KB
/
CGdiPlusBitmap.h
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
#pragma once
class CGdiPlusBitmap
{
public:
Gdiplus::Bitmap* m_pBitmap;
public:
CGdiPlusBitmap() { m_pBitmap = NULL; }
CGdiPlusBitmap(LPCWSTR pFile) { m_pBitmap = NULL; Load(pFile); }
virtual ~CGdiPlusBitmap() { Empty(); }
void Empty() { delete m_pBitmap; m_pBitmap = NULL; }
bool Load(LPCWSTR pFile)
{
Empty();
m_pBitmap = Gdiplus::Bitmap::FromFile(pFile);
return m_pBitmap->GetLastStatus() == Gdiplus::Ok;
}
bool Loads(LPCWSTR pFile)
{
Empty();
m_pBitmap = Gdiplus::Bitmap::FromFile(pFile);
return m_pBitmap->GetLastStatus() == Gdiplus::Ok;
}
operator Gdiplus::Bitmap*() const { return m_pBitmap; }
};
class CGdiPlusBitmapResource : public CGdiPlusBitmap
{
protected:
HGLOBAL m_hBuffer;
public:
CGdiPlusBitmapResource() { m_hBuffer = NULL; }
CGdiPlusBitmapResource(LPCTSTR pName, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL)
{ m_hBuffer = NULL; Load(pName, pType, hInst); }
CGdiPlusBitmapResource(UINT id, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL)
{ m_hBuffer = NULL; Load(id, pType, hInst); }
CGdiPlusBitmapResource(UINT id, UINT type, HMODULE hInst = NULL)
{ m_hBuffer = NULL; Load(id, type, hInst); }
virtual ~CGdiPlusBitmapResource() { Empty(); }
void Empty();
bool Load(LPCTSTR pName, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
bool Load(UINT id, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL)
{ return Load(MAKEINTRESOURCE(id), pType, hInst); }
bool Load(UINT id, UINT type, HMODULE hInst = NULL)
{ return Load(MAKEINTRESOURCE(id), MAKEINTRESOURCE(type), hInst); }
bool LoadRaw(unsigned char* bitmapData, int imageSize)
{
/*bool ret = false;
CString path;
wchar_t wchPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wchPath))
{
path = wchPath;
path += "qrcode.bmp";
}
FILE* f = _wfopen(path.GetBuffer(MAX_PATH), _T("wb"));
if (f != NULL)
{
fwrite(bitmapData, imageSize, 1, f);
fclose(f);
}
m_pBitmap = Gdiplus::Bitmap::FromFile(path);
if (m_pBitmap)
{
Status s = m_pBitmap->GetLastStatus();
if (m_pBitmap->GetLastStatus() != Gdiplus::Ok)
{
delete m_pBitmap;
m_pBitmap = NULL;
}
else
{
ret = true;
}
}
::DeleteFile(path);*/
Empty();
m_hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
if (m_hBuffer)
{
void* pBuffer = ::GlobalLock(m_hBuffer);
if (pBuffer)
{
CopyMemory(pBuffer, bitmapData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK)
{
m_pBitmap = Gdiplus::Bitmap:: FromStream(pStream);
pStream->Release();
if (m_pBitmap)
{
if (m_pBitmap->GetLastStatus() == Gdiplus::Ok)
return true;
delete m_pBitmap;
m_pBitmap = NULL;
}
}
::GlobalUnlock(m_hBuffer);
}
::GlobalFree(m_hBuffer);
m_hBuffer = NULL;
}
return false;
//return ret;
}
};
inline
void CGdiPlusBitmapResource::Empty()
{
CGdiPlusBitmap::Empty();
if (m_hBuffer)
{
::GlobalUnlock(m_hBuffer);
::GlobalFree(m_hBuffer);
m_hBuffer = NULL;
}
}
inline
bool CGdiPlusBitmapResource::Load(LPCTSTR pName, LPCTSTR pType, HMODULE hInst)
{
Empty();
HRSRC hResource = ::FindResource(hInst, pName, pType);
if (!hResource)
return false;
DWORD imageSize = ::SizeofResource(hInst, hResource);
if (!imageSize)
return false;
const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
if (!pResourceData)
return false;
m_hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
if (m_hBuffer)
{
void* pBuffer = ::GlobalLock(m_hBuffer);
if (pBuffer)
{
CopyMemory(pBuffer, pResourceData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK)
{
m_pBitmap = Gdiplus::Bitmap:: FromStream(pStream);
pStream->Release();
if (m_pBitmap)
{
if (m_pBitmap->GetLastStatus() == Gdiplus::Ok)
return true;
delete m_pBitmap;
m_pBitmap = NULL;
}
}
::GlobalUnlock(m_hBuffer);
}
::GlobalFree(m_hBuffer);
m_hBuffer = NULL;
}
return false;
}