質問編集履歴
1
参照の追加、抜け修正用のコードの変更(//以下変更後)
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,6 +30,8 @@
|
|
30
30
|
|
31
31
|
using System.Drawing.Imaging;
|
32
32
|
|
33
|
+
using System.Runtime.InteropServices;//追加
|
34
|
+
|
33
35
|
|
34
36
|
|
35
37
|
namespace image
|
@@ -108,6 +110,186 @@
|
|
108
110
|
|
109
111
|
}
|
110
112
|
|
113
|
+
|
114
|
+
|
115
|
+
//以下変更後
|
116
|
+
|
117
|
+
public Form1()
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
////byte配列をグレースケール画像で保存する////
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
Bitmap empty = new Bitmap(330, 330);//空の画像(入力)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
int Gwidth = 330;
|
130
|
+
|
131
|
+
int Gheight = 330;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
byte[] Gdata = new byte[Gwidth*Gheight];
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
for (int h = 0; h < Gheight; h++)
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
for(int w =0; w < Gwidth; w++)
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
Gdata[h *Gwidth+ w] = (byte)numbers[h*Gwidth+w];
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
unsafe
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
//出力ビットマップの領域確保
|
164
|
+
|
165
|
+
Bitmap Gimg = new Bitmap(empty.Width, empty.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
//出力ビットマップをシステムメモリにロック
|
170
|
+
|
171
|
+
BitmapData dataRgb = empty.LockBits(new Rectangle(0, 0, empty.Width, empty.Height),ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
172
|
+
|
173
|
+
BitmapData bmpdata = Gimg.LockBits(new Rectangle(0, 0, Gimg.Width, Gimg.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
byte* datas24bppRgb = (byte*)dataRgb.Scan0;
|
178
|
+
|
179
|
+
byte* datas8bppGray = (byte*)bmpdata.Scan0;
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
int lineIn = empty.Width * 3;
|
186
|
+
|
187
|
+
int lineInNew = (lineIn % 4 != 0) ? ((lineIn / 4 + 1) * 4) : lineIn;
|
188
|
+
|
189
|
+
int lineInDiff = lineInNew - lineIn;
|
190
|
+
|
191
|
+
int offsetIn = 0;
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
int lineOut = empty.Width;
|
196
|
+
|
197
|
+
int lineOutNew = (lineOut % 4 != 0) ? ((lineOut / 4 + 1) * 4) : lineOut;
|
198
|
+
|
199
|
+
int lineOutDiff = lineOutNew - lineOut;
|
200
|
+
|
201
|
+
int offsetOut = 0;
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
// ポインタを介して、ピクセル毎にグレースケール化
|
210
|
+
|
211
|
+
for (int y = 0; y < empty.Height; y++)
|
212
|
+
|
213
|
+
{
|
214
|
+
|
215
|
+
for (int x = 0; x < empty.Width; x++)
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
// 4バイト境界の考慮
|
220
|
+
|
221
|
+
datas8bppGray[y * empty.Width + x + offsetOut] = (byte)(
|
222
|
+
|
223
|
+
((int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn]) +
|
224
|
+
|
225
|
+
(int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn + 1]) +
|
226
|
+
|
227
|
+
(int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn + 2])) /
|
228
|
+
|
229
|
+
3);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
// 4バイト境界の考慮
|
234
|
+
|
235
|
+
offsetIn += lineInDiff;
|
236
|
+
|
237
|
+
offsetOut += lineOutDiff;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
//パレット情報の設定
|
246
|
+
|
247
|
+
ColorPalette pal = Gimg.Palette;
|
248
|
+
|
249
|
+
for (int i = 0; i < 256; ++i)
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
pal.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
Gimg.Palette = pal;
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
Marshal.Copy(Gdata, 0, bmpdata.Scan0, Gdata.Length);
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
//出力ビットマップのロック解除
|
270
|
+
|
271
|
+
empty.UnlockBits(dataRgb);
|
272
|
+
|
273
|
+
Gimg.UnlockBits(bmpdata);
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
Gimg.Save(@"D:\sources\new3.jpg");
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
////byte配列をグレースケール画像で保存する////
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
}
|
292
|
+
|
111
293
|
```
|
112
294
|
|
113
295
|
|
@@ -119,3 +301,5 @@
|
|
119
301
|
333×333、330×330ピクセルでは抜けが発生しましたが
|
120
302
|
|
121
303
|
250×250、500×300では全てのピクセルに指定した輝度値(225)が割り振られていました。
|
304
|
+
|
305
|
+
2020/1/16追記「//以下変更点」のようにコードを変えてみましたがピクセル抜けが解消されませんでした。
|