回答編集履歴

4

追記

2016/06/03 11:11

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -191,3 +191,115 @@
191
191
  ```
192
192
 
193
193
 
194
+
195
+ ---
196
+
197
+ PictureBoxが重ねられないと勘違いしていたので、改めてプログラムを手直ししてみました。
198
+
199
+ PictureBox2枚重ね版です(pictureBox1が手前で2が背景)。前より少しだけコード量が少なくなりますね。
200
+
201
+ ```C#
202
+
203
+ private void Form1_Load(object sender, EventArgs e)
204
+
205
+ {
206
+
207
+ pictureBox1.Parent = pictureBox2;
208
+
209
+ pictureBox1.Size = pictureBox2.Size;
210
+
211
+ pictureBox1.Location = new Point(0, 0);
212
+
213
+ pictureBox1.BackColor = Color.Transparent;
214
+
215
+
216
+
217
+ int width = pictureBox1.ClientRectangle.Width;
218
+
219
+ int height = pictureBox1.ClientRectangle.Height;
220
+
221
+ pictureBox1.Image = new Bitmap(width, height);
222
+
223
+ pictureBox2.Image = new Bitmap(width, height);
224
+
225
+
226
+
227
+ grfxFg = Graphics.FromImage(pictureBox1.Image);
228
+
229
+ grfxBg = Graphics.FromImage(pictureBox2.Image);
230
+
231
+ }
232
+
233
+
234
+
235
+ Graphics grfxBg; // Graphics オブジェクト
236
+
237
+ Graphics grfxFg; // Graphics オブジェクト
238
+
239
+ int start = 0; // 1 = 始点 確定
240
+
241
+ int startX; // Line X 始点
242
+
243
+ int startY; // Line Y 始点
244
+
245
+ int end = 0; // 1 = 終点 確定
246
+
247
+ int endX; // Line X 終点
248
+
249
+ int endY; // Line Y 終点
250
+
251
+
252
+
253
+ private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
254
+
255
+ {
256
+
257
+ start = 1;
258
+
259
+ startX = e.X;
260
+
261
+ startY = e.Y;
262
+
263
+ }
264
+
265
+
266
+
267
+ private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
268
+
269
+ {
270
+
271
+ if(start == 0)
272
+
273
+ return;
274
+
275
+ endX = e.X;
276
+
277
+ endY = e.Y;
278
+
279
+ // 手前のイメージを透明でクリアして線を描画
280
+
281
+ grfxFg.Clear(Color.FromArgb(0));
282
+
283
+ grfxFg.DrawLine(Pens.Magenta, startX, startY, e.X, e.Y);
284
+
285
+ pictureBox1.Invalidate();
286
+
287
+ }
288
+
289
+
290
+
291
+ private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
292
+
293
+ {
294
+
295
+ start = 0;
296
+
297
+ // 背景のイメージに手前のイメージを合成。線が確定
298
+
299
+ grfxBg.DrawImage(pictureBox1.Image, 0, 0);
300
+
301
+ }
302
+
303
+ ```
304
+
305
+

3

訂正

2016/06/03 11:11

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -65,8 +65,6 @@
65
65
  }
66
66
 
67
67
  ```
68
-
69
- フォームアプリでは`PictureBox`同士を重ねて表示することは、たぶんできないので、`pictureBox2`を作っても役には立たないと思います。不要なので削除してください。
70
68
 
71
69
 
72
70
 

2

追記

2016/06/03 10:45

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -69,3 +69,127 @@
69
69
  フォームアプリでは`PictureBox`同士を重ねて表示することは、たぶんできないので、`pictureBox2`を作っても役には立たないと思います。不要なので削除してください。
70
70
 
71
71
 
72
+
73
+ ---
74
+
75
+ もしかしてこういうことかと思い、ちょっとプログラムを手直ししてみました。
76
+
77
+ マウスボタンを押した位置から離した位置を直線で結ぶような感じです。2枚の絵を合成する方法で描いています。
78
+
79
+ ```C#
80
+
81
+ private void Form1_Load(object sender, EventArgs e)
82
+
83
+ {
84
+
85
+ int width = pictureBox1.ClientRectangle.Width;
86
+
87
+ int height = pictureBox1.ClientRectangle.Height;
88
+
89
+ pictureBox1.Image = new Bitmap(width, height);
90
+
91
+ imageBg = new Bitmap(width, height);
92
+
93
+ imageFg = new Bitmap(width, height);
94
+
95
+ grfx = Graphics.FromImage(pictureBox1.Image);
96
+
97
+ graphBg = Graphics.FromImage(imageBg);
98
+
99
+ graphFg = Graphics.FromImage(imageFg);
100
+
101
+ // とりあえず背景を白でクリア。これをやらないとうまく合成できない
102
+
103
+ graphBg.Clear(Color.White);
104
+
105
+ grfx.DrawImage(imageBg, 0, 0);
106
+
107
+ }
108
+
109
+
110
+
111
+ Bitmap imageBg; // 背景となるイメージ
112
+
113
+ Bitmap imageFg; // 手前のイメージ。ドラッグ中はここに線を描画する
114
+
115
+ Graphics graphBg; // 背景描画用
116
+
117
+ Graphics graphFg; // 手前描画用
118
+
119
+
120
+
121
+ Graphics grfx; // Graphics オブジェクト
122
+
123
+ int start = 0; // 1 = 始点 確定
124
+
125
+ int startX; // Line X 始点
126
+
127
+ int startY; // Line Y 始点
128
+
129
+ int end = 0; // 1 = 終点 確定
130
+
131
+ int endX; // Line X 終点
132
+
133
+ int endY; // Line Y 終点
134
+
135
+
136
+
137
+ private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
138
+
139
+ {
140
+
141
+ start = 1;
142
+
143
+ startX = e.X;
144
+
145
+ startY = e.Y;
146
+
147
+ }
148
+
149
+
150
+
151
+ private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
152
+
153
+ {
154
+
155
+ if(start == 0)
156
+
157
+ return;
158
+
159
+ endX = e.X;
160
+
161
+ endY = e.Y;
162
+
163
+ // 手前のイメージを透明でクリアしてから線を描画
164
+
165
+ graphFg.Clear(Color.FromArgb(0));
166
+
167
+ graphFg.DrawLine(Pens.Magenta, startX, startY, e.X, e.Y);
168
+
169
+ // 背景と合成してPictureBoxのイメージに描画
170
+
171
+ grfx.DrawImage(imageBg, 0, 0);
172
+
173
+ grfx.DrawImage(imageFg, 0, 0);
174
+
175
+ pictureBox1.Invalidate();
176
+
177
+ }
178
+
179
+
180
+
181
+ private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
182
+
183
+ {
184
+
185
+ start = 0;
186
+
187
+ // 背景のイメージに手前のイメージを合成。線が確定
188
+
189
+ graphBg.DrawImage(imageFg, 0, 0);
190
+
191
+ }
192
+
193
+ ```
194
+
195
+

1

追記

2016/06/03 10:26

投稿

catsforepaw
catsforepaw

スコア5938

test CHANGED
@@ -65,3 +65,7 @@
65
65
  }
66
66
 
67
67
  ```
68
+
69
+ フォームアプリでは`PictureBox`同士を重ねて表示することは、たぶんできないので、`pictureBox2`を作っても役には立たないと思います。不要なので削除してください。
70
+
71
+