質問編集履歴
1
参照の追加、抜け修正用のコードの変更(//以下変更後)
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,6 +14,7 @@
|
|
14
14
|
using System;
|
15
15
|
using System.Drawing;
|
16
16
|
using System.Drawing.Imaging;
|
17
|
+
using System.Runtime.InteropServices;//追加
|
17
18
|
|
18
19
|
namespace image
|
19
20
|
{
|
@@ -53,9 +54,100 @@
|
|
53
54
|
}
|
54
55
|
}
|
55
56
|
}
|
57
|
+
|
58
|
+
//以下変更後
|
59
|
+
public Form1()
|
60
|
+
{
|
61
|
+
////byte配列をグレースケール画像で保存する////
|
62
|
+
|
63
|
+
Bitmap empty = new Bitmap(330, 330);//空の画像(入力)
|
64
|
+
|
65
|
+
int Gwidth = 330;
|
66
|
+
int Gheight = 330;
|
67
|
+
|
68
|
+
byte[] Gdata = new byte[Gwidth*Gheight];
|
69
|
+
|
70
|
+
for (int h = 0; h < Gheight; h++)
|
71
|
+
{
|
72
|
+
for(int w =0; w < Gwidth; w++)
|
73
|
+
{
|
74
|
+
Gdata[h *Gwidth+ w] = (byte)numbers[h*Gwidth+w];
|
75
|
+
}
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
unsafe
|
80
|
+
{
|
81
|
+
|
82
|
+
//出力ビットマップの領域確保
|
83
|
+
Bitmap Gimg = new Bitmap(empty.Width, empty.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
84
|
+
|
85
|
+
//出力ビットマップをシステムメモリにロック
|
86
|
+
BitmapData dataRgb = empty.LockBits(new Rectangle(0, 0, empty.Width, empty.Height),ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
87
|
+
BitmapData bmpdata = Gimg.LockBits(new Rectangle(0, 0, Gimg.Width, Gimg.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
|
88
|
+
|
89
|
+
byte* datas24bppRgb = (byte*)dataRgb.Scan0;
|
90
|
+
byte* datas8bppGray = (byte*)bmpdata.Scan0;
|
91
|
+
|
92
|
+
|
93
|
+
int lineIn = empty.Width * 3;
|
94
|
+
int lineInNew = (lineIn % 4 != 0) ? ((lineIn / 4 + 1) * 4) : lineIn;
|
95
|
+
int lineInDiff = lineInNew - lineIn;
|
96
|
+
int offsetIn = 0;
|
97
|
+
|
98
|
+
int lineOut = empty.Width;
|
99
|
+
int lineOutNew = (lineOut % 4 != 0) ? ((lineOut / 4 + 1) * 4) : lineOut;
|
100
|
+
int lineOutDiff = lineOutNew - lineOut;
|
101
|
+
int offsetOut = 0;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// ポインタを介して、ピクセル毎にグレースケール化
|
106
|
+
for (int y = 0; y < empty.Height; y++)
|
107
|
+
{
|
108
|
+
for (int x = 0; x < empty.Width; x++)
|
109
|
+
{
|
110
|
+
// 4バイト境界の考慮
|
111
|
+
datas8bppGray[y * empty.Width + x + offsetOut] = (byte)(
|
112
|
+
((int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn]) +
|
113
|
+
(int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn + 1]) +
|
114
|
+
(int)(datas24bppRgb[y * empty.Width * 3 + x * 3 + offsetIn + 2])) /
|
115
|
+
3);
|
116
|
+
}
|
117
|
+
// 4バイト境界の考慮
|
118
|
+
offsetIn += lineInDiff;
|
119
|
+
offsetOut += lineOutDiff;
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
//パレット情報の設定
|
124
|
+
ColorPalette pal = Gimg.Palette;
|
125
|
+
for (int i = 0; i < 256; ++i)
|
126
|
+
{
|
127
|
+
pal.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
|
128
|
+
|
129
|
+
}
|
130
|
+
Gimg.Palette = pal;
|
131
|
+
|
132
|
+
|
133
|
+
Marshal.Copy(Gdata, 0, bmpdata.Scan0, Gdata.Length);
|
134
|
+
|
135
|
+
//出力ビットマップのロック解除
|
136
|
+
empty.UnlockBits(dataRgb);
|
137
|
+
Gimg.UnlockBits(bmpdata);
|
138
|
+
|
139
|
+
|
140
|
+
Gimg.Save(@"D:\sources\new3.jpg");
|
141
|
+
|
142
|
+
}
|
143
|
+
|
144
|
+
////byte配列をグレースケール画像で保存する////
|
145
|
+
|
146
|
+
}
|
56
147
|
```
|
57
148
|
|
58
149
|
### 試したこと
|
59
150
|
|
60
151
|
333×333、330×330ピクセルでは抜けが発生しましたが
|
61
|
-
250×250、500×300では全てのピクセルに指定した輝度値(225)が割り振られていました。
|
152
|
+
250×250、500×300では全てのピクセルに指定した輝度値(225)が割り振られていました。
|
153
|
+
2020/1/16追記「//以下変更点」のようにコードを変えてみましたがピクセル抜けが解消されませんでした。
|