回答編集履歴

2

見直しキャンペーン中

2023/07/17 12:47

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -2,7 +2,7 @@
2
2
  を参考にZoom専用で省コード化。
3
3
  端数を切り捨てているからか気持ちずれているような気もする。
4
4
 
5
- ```cs
5
+ ```cs:Form1.cs
6
6
  using System;
7
7
  using System.Diagnostics;
8
8
  using System.Drawing;
@@ -93,7 +93,7 @@
93
93
 
94
94
  今回は違ったようですが言及してしまったので、閲覧者向けにカーソル下の色の取得の楽なソリューション例。
95
95
 
96
- ```cs
96
+ ```cs:Form1.cs
97
97
  using System;
98
98
  using System.ComponentModel;
99
99
  using System.Drawing;
@@ -123,7 +123,7 @@
123
123
  }
124
124
  ```
125
125
 
126
- ```cs
126
+ ```cs:Form1.Designer.cs
127
127
  namespace Questions230110
128
128
  {
129
129
  partial class Form1

1

リンク切れ

2022/09/15 10:45

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,453 +1,227 @@
1
- [Select parts of a scaled image with different SizeMode valuesC# Helper ](http://csharphelper.com/blog/2014/10/select-parts-of-a-scaled-image-picturebox-different-sizemode-values-c/)
2
-
1
+ [Select parts of a scaled image with different SizeMode valuesC# Helper](https://web.archive.org/web/20210415145016/http://csharphelper.com/blog/2014/10/select-parts-of-a-scaled-image-picturebox-different-sizemode-values-c/)
3
2
  を参考にZoom専用で省コード化。
4
-
5
3
  端数を切り捨てているからか気持ちずれているような気もする。
6
4
 
7
-
8
-
9
- ```C#
5
+ ```cs
10
-
11
6
  using System;
12
-
13
7
  using System.Diagnostics;
14
-
15
8
  using System.Drawing;
16
-
17
9
  using System.Windows.Forms;
18
10
 
19
-
20
-
21
11
  namespace Questions230110
22
-
23
12
  {
24
-
25
13
  public partial class Form1 : Form
26
-
27
14
  {
28
-
29
15
  private System.Windows.Forms.PictureBox pictureBox1;
30
16
 
31
-
32
-
33
17
  public Form1()
34
-
35
- {
18
+ {
36
-
37
19
  //InitializeComponent();
38
-
39
20
  this.pictureBox1 = new System.Windows.Forms.PictureBox();
40
-
41
21
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
42
-
43
22
  this.SuspendLayout();
44
-
45
- //
23
+ //
46
-
47
24
  // pictureBox1
48
-
49
- //
25
+ //
50
-
51
26
  this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
52
-
53
27
  this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
54
-
55
28
  this.pictureBox1.Location = new System.Drawing.Point(0, 0);
56
-
57
29
  this.pictureBox1.Name = "pictureBox1";
58
-
59
30
  this.pictureBox1.Size = new System.Drawing.Size(800, 450);
60
-
61
31
  this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
62
-
63
32
  this.pictureBox1.TabIndex = 0;
64
-
65
33
  this.pictureBox1.TabStop = false;
66
-
67
34
  this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
68
-
69
- //
35
+ //
70
-
71
36
  // Form1
72
-
73
- //
37
+ //
74
-
75
38
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
76
-
77
39
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
78
-
79
40
  this.ClientSize = new System.Drawing.Size(800, 450);
80
-
81
41
  this.Controls.Add(this.pictureBox1);
82
-
83
42
  this.Name = "Form1";
84
-
85
43
  this.Text = "Form1";
86
-
87
44
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
88
-
89
45
  this.ResumeLayout(false);
90
-
91
- }
46
+ }
92
-
93
-
94
47
 
95
48
  private void pictureBox1_Click(object sender, EventArgs e)
96
-
97
- {
49
+ {
98
-
99
50
  var p = ConvertCoordinates(((MouseEventArgs)e).Location);
100
-
101
51
  Debug.WriteLine(p);
102
-
103
- }
52
+ }
104
-
105
53
  private Point ConvertCoordinates(Point location)
106
-
107
- {
54
+ {
108
-
109
55
  var x = location.X;
110
-
111
56
  var y = location.Y;
112
-
113
57
  var picH = pictureBox1.ClientSize.Height;
114
-
115
58
  var picW = pictureBox1.ClientSize.Width;
116
-
117
59
  var imgH = pictureBox1.Image.Height;
118
-
119
60
  var imgW = pictureBox1.Image.Width;
120
61
 
121
-
122
-
123
62
  int X0;
124
-
125
63
  int Y0;
126
-
127
64
  if(picW / (float)picH > imgW / (float)imgH)
128
-
129
- {
65
+ {
130
-
131
66
  var scaledW = imgW * picH / (float)imgH;
132
-
133
67
  var dx = (picW - scaledW) / 2;
134
-
135
68
  X0 = (int)((x - dx) * imgH / picH);
136
69
 
137
-
138
-
139
70
  Y0 = (int)(imgH * y / (float)picH);
140
-
141
- }
71
+ }
142
-
143
72
  else
144
-
145
- {
73
+ {
146
-
147
74
  X0 = (int)(imgW * x / (float)picW);
148
75
 
149
-
150
-
151
76
  var scaledH = imgH * picW / (float)imgW;
152
-
153
77
  var dy = (picH - scaledH) / 2;
154
-
155
78
  Y0 = (int)((y - dy) * imgW / picW);
156
-
157
- }
79
+ }
158
-
159
-
160
80
 
161
81
  if(X0 < 0 || imgW < X0 || Y0 < 0 || imgH < Y0)
162
-
163
- {
82
+ {
164
-
165
83
  return new Point(-1, -1); // 範囲外をどう表すのがいいか
166
-
167
- }
84
+ }
168
-
169
-
170
85
 
171
86
  return new Point(X0, Y0);
172
-
173
- }
87
+ }
174
-
175
88
  }
176
-
177
89
  }
178
-
179
90
  ```
180
91
 
181
-
182
-
183
92
  ---
184
93
 
185
-
186
-
187
94
  今回は違ったようですが言及してしまったので、閲覧者向けにカーソル下の色の取得の楽なソリューション例。
188
95
 
189
-
190
-
191
- ```C#
96
+ ```cs
192
-
193
97
  using System;
194
-
195
98
  using System.ComponentModel;
196
-
197
99
  using System.Drawing;
198
-
199
100
  using System.Windows.Forms;
200
101
 
201
-
202
-
203
102
  namespace Questions230110
204
-
205
103
  {
206
-
207
104
  public partial class Form1 : Form
208
-
209
105
  {
210
-
211
106
  private Bitmap bmp;
212
-
213
107
  public Form1() => InitializeComponent();
214
108
 
215
-
216
-
217
109
  private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
218
-
219
- {
110
+ {
220
-
221
111
  if(bmp == null) return;
222
-
223
112
  panel1.BackColor = bmp.GetPixel(e.Location.X, e.Location.Y);
224
-
225
- }
113
+ }
226
-
227
-
228
114
 
229
115
  private void PictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) => DrawToBitmap();
230
-
231
116
  private void PictureBox1_Resize(object sender, EventArgs e) => DrawToBitmap();
232
-
233
117
  private void DrawToBitmap()
234
-
235
- {
118
+ {
236
-
237
119
  bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
238
-
239
120
  pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
240
-
241
- }
121
+ }
242
-
243
122
  }
244
-
245
123
  }
246
-
247
124
  ```
248
125
 
249
-
250
-
251
- ```C#
126
+ ```cs
252
-
253
127
  namespace Questions230110
254
-
255
128
  {
256
-
257
129
  partial class Form1
258
-
259
130
  {
260
-
261
131
  /// <summary>
262
-
263
132
  /// 必要なデザイナー変数です。
264
-
265
133
  /// </summary>
266
-
267
134
  private System.ComponentModel.IContainer components = null;
268
135
 
269
-
270
-
271
136
  /// <summary>
272
-
273
137
  /// 使用中のリソースをすべてクリーンアップします。
274
-
275
138
  /// </summary>
276
-
277
139
  /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
278
-
279
140
  protected override void Dispose(bool disposing)
280
-
281
- {
141
+ {
282
-
283
142
  if(disposing && (components != null))
284
-
285
- {
143
+ {
286
-
287
144
  components.Dispose();
288
-
289
- }
145
+ }
290
-
291
146
  base.Dispose(disposing);
292
-
293
- }
147
+ }
294
-
295
-
296
148
 
297
149
  #region Windows フォーム デザイナーで生成されたコード
298
150
 
299
-
300
-
301
151
  /// <summary>
302
-
303
152
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
304
-
305
153
  /// コード エディターで変更しないでください。
306
-
307
154
  /// </summary>
308
-
309
155
  private void InitializeComponent()
310
-
311
- {
156
+ {
312
-
313
157
  this.panel1 = new System.Windows.Forms.Panel();
314
-
315
158
  this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
316
-
317
159
  this.pictureBox1 = new System.Windows.Forms.PictureBox();
318
-
319
160
  this.tableLayoutPanel1.SuspendLayout();
320
-
321
161
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
322
-
323
162
  this.SuspendLayout();
324
-
325
- //
163
+ //
326
-
327
164
  // panel1
328
-
329
- //
165
+ //
330
-
331
166
  this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
332
-
333
167
  this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
334
-
335
168
  this.panel1.Location = new System.Drawing.Point(375, 6);
336
-
337
169
  this.panel1.Name = "panel1";
338
-
339
170
  this.panel1.Size = new System.Drawing.Size(50, 50);
340
-
341
171
  this.panel1.TabIndex = 0;
342
-
343
- //
172
+ //
344
-
345
173
  // tableLayoutPanel1
346
-
347
- //
174
+ //
348
-
349
175
  this.tableLayoutPanel1.ColumnCount = 3;
350
-
351
176
  this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
352
-
353
177
  this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
354
-
355
178
  this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
356
-
357
179
  this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 1);
358
-
359
180
  this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
360
-
361
181
  this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
362
-
363
182
  this.tableLayoutPanel1.Name = "tableLayoutPanel1";
364
-
365
183
  this.tableLayoutPanel1.RowCount = 3;
366
-
367
184
  this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
368
-
369
185
  this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
370
-
371
186
  this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
372
-
373
187
  this.tableLayoutPanel1.Size = new System.Drawing.Size(800, 63);
374
-
375
188
  this.tableLayoutPanel1.TabIndex = 3;
376
-
377
- //
189
+ //
378
-
379
190
  // pictureBox1
380
-
381
- //
191
+ //
382
-
383
192
  this.pictureBox1.BackColor = System.Drawing.Color.Black;
384
-
385
193
  this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
386
-
387
194
  this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
388
-
389
195
  this.pictureBox1.Location = new System.Drawing.Point(0, 0);
390
-
391
196
  this.pictureBox1.Name = "pictureBox1";
392
-
393
197
  this.pictureBox1.Size = new System.Drawing.Size(800, 450);
394
-
395
198
  this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
396
-
397
199
  this.pictureBox1.TabIndex = 0;
398
-
399
200
  this.pictureBox1.TabStop = false;
400
-
401
201
  this.pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.PictureBox1_LoadCompleted);
402
-
403
202
  this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
404
-
405
203
  this.pictureBox1.Resize += new System.EventHandler(this.PictureBox1_Resize);
406
-
407
- //
204
+ //
408
-
409
205
  // Form1
410
-
411
- //
206
+ //
412
-
413
207
  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
414
-
415
208
  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
416
-
417
209
  this.ClientSize = new System.Drawing.Size(800, 450);
418
-
419
210
  this.Controls.Add(this.tableLayoutPanel1);
420
-
421
211
  this.Controls.Add(this.pictureBox1);
422
-
423
212
  this.Name = "Form1";
424
-
425
213
  this.Text = "Form1";
426
-
427
214
  this.tableLayoutPanel1.ResumeLayout(false);
428
-
429
215
  ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
430
-
431
216
  this.ResumeLayout(false);
432
217
 
433
-
434
-
435
- }
218
+ }
436
-
437
-
438
219
 
439
220
  #endregion
440
221
 
441
-
442
-
443
222
  private System.Windows.Forms.PictureBox pictureBox1;
444
-
445
223
  private System.Windows.Forms.Panel panel1;
446
-
447
224
  private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
448
-
449
225
  }
450
-
451
226
  }
452
-
453
227
  ```