teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/07/17 12:47

投稿

TN8001
TN8001

スコア10108

answer 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

スコア10108

answer CHANGED
@@ -1,227 +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
- を参考にZoom専用で省コード化。
3
- 端数を切り捨てているからか気持ちずれているような気もする。
4
-
5
- ```C#
6
- using System;
7
- using System.Diagnostics;
8
- using System.Drawing;
9
- using System.Windows.Forms;
10
-
11
- namespace Questions230110
12
- {
13
- public partial class Form1 : Form
14
- {
15
- private System.Windows.Forms.PictureBox pictureBox1;
16
-
17
- public Form1()
18
- {
19
- //InitializeComponent();
20
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
21
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
22
- this.SuspendLayout();
23
- //
24
- // pictureBox1
25
- //
26
- this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
27
- this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
28
- this.pictureBox1.Location = new System.Drawing.Point(0, 0);
29
- this.pictureBox1.Name = "pictureBox1";
30
- this.pictureBox1.Size = new System.Drawing.Size(800, 450);
31
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
32
- this.pictureBox1.TabIndex = 0;
33
- this.pictureBox1.TabStop = false;
34
- this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
35
- //
36
- // Form1
37
- //
38
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
39
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
40
- this.ClientSize = new System.Drawing.Size(800, 450);
41
- this.Controls.Add(this.pictureBox1);
42
- this.Name = "Form1";
43
- this.Text = "Form1";
44
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
45
- this.ResumeLayout(false);
46
- }
47
-
48
- private void pictureBox1_Click(object sender, EventArgs e)
49
- {
50
- var p = ConvertCoordinates(((MouseEventArgs)e).Location);
51
- Debug.WriteLine(p);
52
- }
53
- private Point ConvertCoordinates(Point location)
54
- {
55
- var x = location.X;
56
- var y = location.Y;
57
- var picH = pictureBox1.ClientSize.Height;
58
- var picW = pictureBox1.ClientSize.Width;
59
- var imgH = pictureBox1.Image.Height;
60
- var imgW = pictureBox1.Image.Width;
61
-
62
- int X0;
63
- int Y0;
64
- if(picW / (float)picH > imgW / (float)imgH)
65
- {
66
- var scaledW = imgW * picH / (float)imgH;
67
- var dx = (picW - scaledW) / 2;
68
- X0 = (int)((x - dx) * imgH / picH);
69
-
70
- Y0 = (int)(imgH * y / (float)picH);
71
- }
72
- else
73
- {
74
- X0 = (int)(imgW * x / (float)picW);
75
-
76
- var scaledH = imgH * picW / (float)imgW;
77
- var dy = (picH - scaledH) / 2;
78
- Y0 = (int)((y - dy) * imgW / picW);
79
- }
80
-
81
- if(X0 < 0 || imgW < X0 || Y0 < 0 || imgH < Y0)
82
- {
83
- return new Point(-1, -1); // 範囲外をどう表すのがいいか
84
- }
85
-
86
- return new Point(X0, Y0);
87
- }
88
- }
89
- }
90
- ```
91
-
92
- ---
93
-
94
- 今回は違ったようですが言及してしまったので、閲覧者向けにカーソル下の色の取得の楽なソリューション例。
95
-
96
- ```C#
97
- using System;
98
- using System.ComponentModel;
99
- using System.Drawing;
100
- using System.Windows.Forms;
101
-
102
- namespace Questions230110
103
- {
104
- public partial class Form1 : Form
105
- {
106
- private Bitmap bmp;
107
- public Form1() => InitializeComponent();
108
-
109
- private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
110
- {
111
- if(bmp == null) return;
112
- panel1.BackColor = bmp.GetPixel(e.Location.X, e.Location.Y);
113
- }
114
-
115
- private void PictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) => DrawToBitmap();
116
- private void PictureBox1_Resize(object sender, EventArgs e) => DrawToBitmap();
117
- private void DrawToBitmap()
118
- {
119
- bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
120
- pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
121
- }
122
- }
123
- }
124
- ```
125
-
126
- ```C#
127
- namespace Questions230110
128
- {
129
- partial class Form1
130
- {
131
- /// <summary>
132
- /// 必要なデザイナー変数です。
133
- /// </summary>
134
- private System.ComponentModel.IContainer components = null;
135
-
136
- /// <summary>
137
- /// 使用中のリソースをすべてクリーンアップします。
138
- /// </summary>
139
- /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
140
- protected override void Dispose(bool disposing)
141
- {
142
- if(disposing && (components != null))
143
- {
144
- components.Dispose();
145
- }
146
- base.Dispose(disposing);
147
- }
148
-
149
- #region Windows フォーム デザイナーで生成されたコード
150
-
151
- /// <summary>
152
- /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
153
- /// コード エディターで変更しないでください。
154
- /// </summary>
155
- private void InitializeComponent()
156
- {
157
- this.panel1 = new System.Windows.Forms.Panel();
158
- this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
159
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
160
- this.tableLayoutPanel1.SuspendLayout();
161
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
162
- this.SuspendLayout();
163
- //
164
- // panel1
165
- //
166
- this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
167
- this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
168
- this.panel1.Location = new System.Drawing.Point(375, 6);
169
- this.panel1.Name = "panel1";
170
- this.panel1.Size = new System.Drawing.Size(50, 50);
171
- this.panel1.TabIndex = 0;
172
- //
173
- // tableLayoutPanel1
174
- //
175
- this.tableLayoutPanel1.ColumnCount = 3;
176
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
177
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
178
- this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
179
- this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 1);
180
- this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
181
- this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
182
- this.tableLayoutPanel1.Name = "tableLayoutPanel1";
183
- this.tableLayoutPanel1.RowCount = 3;
184
- this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
185
- this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
186
- this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
187
- this.tableLayoutPanel1.Size = new System.Drawing.Size(800, 63);
188
- this.tableLayoutPanel1.TabIndex = 3;
189
- //
190
- // pictureBox1
191
- //
192
- this.pictureBox1.BackColor = System.Drawing.Color.Black;
193
- this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
194
- this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
195
- this.pictureBox1.Location = new System.Drawing.Point(0, 0);
196
- this.pictureBox1.Name = "pictureBox1";
197
- this.pictureBox1.Size = new System.Drawing.Size(800, 450);
198
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
199
- this.pictureBox1.TabIndex = 0;
200
- this.pictureBox1.TabStop = false;
201
- this.pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.PictureBox1_LoadCompleted);
202
- this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
203
- this.pictureBox1.Resize += new System.EventHandler(this.PictureBox1_Resize);
204
- //
205
- // Form1
206
- //
207
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
208
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
209
- this.ClientSize = new System.Drawing.Size(800, 450);
210
- this.Controls.Add(this.tableLayoutPanel1);
211
- this.Controls.Add(this.pictureBox1);
212
- this.Name = "Form1";
213
- this.Text = "Form1";
214
- this.tableLayoutPanel1.ResumeLayout(false);
215
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
216
- this.ResumeLayout(false);
217
-
218
- }
219
-
220
- #endregion
221
-
222
- private System.Windows.Forms.PictureBox pictureBox1;
223
- private System.Windows.Forms.Panel panel1;
224
- private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
225
- }
226
- }
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/)
2
+ を参考にZoom専用で省コード化。
3
+ 端数を切り捨てているからか気持ちずれているような気もする。
4
+
5
+ ```cs
6
+ using System;
7
+ using System.Diagnostics;
8
+ using System.Drawing;
9
+ using System.Windows.Forms;
10
+
11
+ namespace Questions230110
12
+ {
13
+ public partial class Form1 : Form
14
+ {
15
+ private System.Windows.Forms.PictureBox pictureBox1;
16
+
17
+ public Form1()
18
+ {
19
+ //InitializeComponent();
20
+ this.pictureBox1 = new System.Windows.Forms.PictureBox();
21
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
22
+ this.SuspendLayout();
23
+ //
24
+ // pictureBox1
25
+ //
26
+ this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
27
+ this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
28
+ this.pictureBox1.Location = new System.Drawing.Point(0, 0);
29
+ this.pictureBox1.Name = "pictureBox1";
30
+ this.pictureBox1.Size = new System.Drawing.Size(800, 450);
31
+ this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
32
+ this.pictureBox1.TabIndex = 0;
33
+ this.pictureBox1.TabStop = false;
34
+ this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
35
+ //
36
+ // Form1
37
+ //
38
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
39
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
40
+ this.ClientSize = new System.Drawing.Size(800, 450);
41
+ this.Controls.Add(this.pictureBox1);
42
+ this.Name = "Form1";
43
+ this.Text = "Form1";
44
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
45
+ this.ResumeLayout(false);
46
+ }
47
+
48
+ private void pictureBox1_Click(object sender, EventArgs e)
49
+ {
50
+ var p = ConvertCoordinates(((MouseEventArgs)e).Location);
51
+ Debug.WriteLine(p);
52
+ }
53
+ private Point ConvertCoordinates(Point location)
54
+ {
55
+ var x = location.X;
56
+ var y = location.Y;
57
+ var picH = pictureBox1.ClientSize.Height;
58
+ var picW = pictureBox1.ClientSize.Width;
59
+ var imgH = pictureBox1.Image.Height;
60
+ var imgW = pictureBox1.Image.Width;
61
+
62
+ int X0;
63
+ int Y0;
64
+ if(picW / (float)picH > imgW / (float)imgH)
65
+ {
66
+ var scaledW = imgW * picH / (float)imgH;
67
+ var dx = (picW - scaledW) / 2;
68
+ X0 = (int)((x - dx) * imgH / picH);
69
+
70
+ Y0 = (int)(imgH * y / (float)picH);
71
+ }
72
+ else
73
+ {
74
+ X0 = (int)(imgW * x / (float)picW);
75
+
76
+ var scaledH = imgH * picW / (float)imgW;
77
+ var dy = (picH - scaledH) / 2;
78
+ Y0 = (int)((y - dy) * imgW / picW);
79
+ }
80
+
81
+ if(X0 < 0 || imgW < X0 || Y0 < 0 || imgH < Y0)
82
+ {
83
+ return new Point(-1, -1); // 範囲外をどう表すのがいいか
84
+ }
85
+
86
+ return new Point(X0, Y0);
87
+ }
88
+ }
89
+ }
90
+ ```
91
+
92
+ ---
93
+
94
+ 今回は違ったようですが言及してしまったので、閲覧者向けにカーソル下の色の取得の楽なソリューション例。
95
+
96
+ ```cs
97
+ using System;
98
+ using System.ComponentModel;
99
+ using System.Drawing;
100
+ using System.Windows.Forms;
101
+
102
+ namespace Questions230110
103
+ {
104
+ public partial class Form1 : Form
105
+ {
106
+ private Bitmap bmp;
107
+ public Form1() => InitializeComponent();
108
+
109
+ private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
110
+ {
111
+ if(bmp == null) return;
112
+ panel1.BackColor = bmp.GetPixel(e.Location.X, e.Location.Y);
113
+ }
114
+
115
+ private void PictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) => DrawToBitmap();
116
+ private void PictureBox1_Resize(object sender, EventArgs e) => DrawToBitmap();
117
+ private void DrawToBitmap()
118
+ {
119
+ bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
120
+ pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ ```cs
127
+ namespace Questions230110
128
+ {
129
+ partial class Form1
130
+ {
131
+ /// <summary>
132
+ /// 必要なデザイナー変数です。
133
+ /// </summary>
134
+ private System.ComponentModel.IContainer components = null;
135
+
136
+ /// <summary>
137
+ /// 使用中のリソースをすべてクリーンアップします。
138
+ /// </summary>
139
+ /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
140
+ protected override void Dispose(bool disposing)
141
+ {
142
+ if(disposing && (components != null))
143
+ {
144
+ components.Dispose();
145
+ }
146
+ base.Dispose(disposing);
147
+ }
148
+
149
+ #region Windows フォーム デザイナーで生成されたコード
150
+
151
+ /// <summary>
152
+ /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
153
+ /// コード エディターで変更しないでください。
154
+ /// </summary>
155
+ private void InitializeComponent()
156
+ {
157
+ this.panel1 = new System.Windows.Forms.Panel();
158
+ this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
159
+ this.pictureBox1 = new System.Windows.Forms.PictureBox();
160
+ this.tableLayoutPanel1.SuspendLayout();
161
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
162
+ this.SuspendLayout();
163
+ //
164
+ // panel1
165
+ //
166
+ this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
167
+ this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
168
+ this.panel1.Location = new System.Drawing.Point(375, 6);
169
+ this.panel1.Name = "panel1";
170
+ this.panel1.Size = new System.Drawing.Size(50, 50);
171
+ this.panel1.TabIndex = 0;
172
+ //
173
+ // tableLayoutPanel1
174
+ //
175
+ this.tableLayoutPanel1.ColumnCount = 3;
176
+ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
177
+ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
178
+ this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
179
+ this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 1);
180
+ this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
181
+ this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
182
+ this.tableLayoutPanel1.Name = "tableLayoutPanel1";
183
+ this.tableLayoutPanel1.RowCount = 3;
184
+ this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
185
+ this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
186
+ this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
187
+ this.tableLayoutPanel1.Size = new System.Drawing.Size(800, 63);
188
+ this.tableLayoutPanel1.TabIndex = 3;
189
+ //
190
+ // pictureBox1
191
+ //
192
+ this.pictureBox1.BackColor = System.Drawing.Color.Black;
193
+ this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
194
+ this.pictureBox1.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/1/1c/City_view_with_blue_sky.jpeg";
195
+ this.pictureBox1.Location = new System.Drawing.Point(0, 0);
196
+ this.pictureBox1.Name = "pictureBox1";
197
+ this.pictureBox1.Size = new System.Drawing.Size(800, 450);
198
+ this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
199
+ this.pictureBox1.TabIndex = 0;
200
+ this.pictureBox1.TabStop = false;
201
+ this.pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.PictureBox1_LoadCompleted);
202
+ this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
203
+ this.pictureBox1.Resize += new System.EventHandler(this.PictureBox1_Resize);
204
+ //
205
+ // Form1
206
+ //
207
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
208
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
209
+ this.ClientSize = new System.Drawing.Size(800, 450);
210
+ this.Controls.Add(this.tableLayoutPanel1);
211
+ this.Controls.Add(this.pictureBox1);
212
+ this.Name = "Form1";
213
+ this.Text = "Form1";
214
+ this.tableLayoutPanel1.ResumeLayout(false);
215
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
216
+ this.ResumeLayout(false);
217
+
218
+ }
219
+
220
+ #endregion
221
+
222
+ private System.Windows.Forms.PictureBox pictureBox1;
223
+ private System.Windows.Forms.Panel panel1;
224
+ private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
225
+ }
226
+ }
227
227
  ```