回答編集履歴

2

windows.hが抜けてたので追加

2018/06/11 03:55

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -11,6 +11,8 @@
11
11
  GDI+で書いたサンプル
12
12
 
13
13
  ```c++
14
+
15
+ #include <windows.h>
14
16
 
15
17
  #include <gdiplus.h>
16
18
 

1

サンプルコード追加

2018/06/11 03:54

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -3,3 +3,179 @@
3
3
  CImageクラスはVS2010でも利用可能です。コンパイルできないとすればそれは何か他に原因があるので、まずはそれを解消すべきでしょう。
4
4
 
5
5
  念のため、たまたまVS2010をインストールした環境が手元に残っていたのでリンク先のコード(私が書いたものですが)を試してみましたが、そのままコピペしてビルドできましたし、正しく実行できていました(保存した画像ファイルで確認)。
6
+
7
+
8
+
9
+ ---
10
+
11
+ GDI+で書いたサンプル
12
+
13
+ ```c++
14
+
15
+ #include <gdiplus.h>
16
+
17
+ #pragma comment (lib,"gdiplus.lib")
18
+
19
+ using namespace Gdiplus;
20
+
21
+
22
+
23
+ // クリップボードからビットマップを取得する関数
24
+
25
+ void SampleClipboard()
26
+
27
+ {
28
+
29
+ if(!::IsClipboardFormatAvailable(CF_BITMAP))
30
+
31
+ {
32
+
33
+ return;
34
+
35
+ }
36
+
37
+
38
+
39
+ if(!::OpenClipboard(NULL))
40
+
41
+ {
42
+
43
+ return;
44
+
45
+ }
46
+
47
+
48
+
49
+ HBITMAP hBitmap = (HBITMAP)::GetClipboardData(CF_BITMAP);
50
+
51
+ if(hBitmap == NULL)
52
+
53
+ {
54
+
55
+ ::CloseClipboard();
56
+
57
+ return;
58
+
59
+ }
60
+
61
+
62
+
63
+ Bitmap* pClip = Bitmap::FromHBITMAP(hBitmap, NULL);
64
+
65
+ Bitmap* pBitmap = pClip->Clone(0, 0, pClip->GetWidth(), pClip->GetHeight(), PixelFormat32bppRGB);
66
+
67
+ delete pClip;
68
+
69
+
70
+
71
+ ::CloseClipboard();
72
+
73
+
74
+
75
+ for(UINT y = 0; y < pBitmap->GetHeight(); y++)
76
+
77
+ {
78
+
79
+ for(UINT x = 0; x < pBitmap->GetWidth(); x++)
80
+
81
+ {
82
+
83
+ Color color;
84
+
85
+ pBitmap->GetPixel(x, y, &color);
86
+
87
+ BYTE r = color.GetR();
88
+
89
+ BYTE g = color.GetG();
90
+
91
+ BYTE b = color.GetB();
92
+
93
+
94
+
95
+ // 何かの処理
96
+
97
+ }
98
+
99
+ }
100
+
101
+
102
+
103
+ // 保存はCImageほど簡単ではない
104
+
105
+ UINT numEncoders;
106
+
107
+ UINT size;
108
+
109
+ GetImageEncodersSize(&numEncoders, &size);
110
+
111
+ ImageCodecInfo* pCodecs = (ImageCodecInfo*)malloc(size);
112
+
113
+ GetImageEncoders(numEncoders, size, pCodecs);
114
+
115
+ ImageCodecInfo* pPngCodec = NULL;
116
+
117
+ for(UINT i = 0; i < numEncoders; i++)
118
+
119
+ {
120
+
121
+ ImageCodecInfo* pCodec = &pCodecs[i];
122
+
123
+ if(wcscmp(pCodec->MimeType, L"image/png") == 0)
124
+
125
+ {
126
+
127
+ pPngCodec = pCodec;
128
+
129
+ break;
130
+
131
+ }
132
+
133
+ }
134
+
135
+ if(pPngCodec != NULL)
136
+
137
+ {
138
+
139
+ pBitmap->Save(L"sample.png", &pPngCodec->Clsid);
140
+
141
+ }
142
+
143
+ free(pCodecs);
144
+
145
+ delete pBitmap;
146
+
147
+ }
148
+
149
+
150
+
151
+
152
+
153
+ int main()
154
+
155
+ {
156
+
157
+ // GDI+の開始
158
+
159
+ GdiplusStartupInput startupInput;
160
+
161
+ ULONG_PTR token;
162
+
163
+ GdiplusStartup(&token, &startupInput, NULL);
164
+
165
+
166
+
167
+ SampleClipboard();
168
+
169
+
170
+
171
+ // GDI+の終了
172
+
173
+ GdiplusShutdown(token);
174
+
175
+ return 0;
176
+
177
+ }
178
+
179
+ ```
180
+
181
+ GDI+を使う場合は、上記main関数のような開始処理と終了処理が必要です。