質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

解決済

2回答

1888閲覧

C#で作成したが、Combobox等が実装されない

great-degin

総合スコア9

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2020/04/17 15:03

編集2020/04/17 15:21

画像を取りこみ、その画像に処理をかけるプログラムをVisual Studio 2019で作成しました。
配られたお手本のソースコードに描いてある通りに作成したはずなんですが、
comboBox1
openFileDialog1
pictureBox1
saveFileDialog1
pictureBox2
textBox1
の部分にエラーメッセージ(CS0103)が表示され、現在のコンテキストに存在しないと出ます。

一体何がいけないのでしょうか?

エラーメッセージ

重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態 エラー CS0103 現在のコンテキストに 'comboBox1' という名前は存在しません。 WindowsFormsApp4 C:\Users\me\source\repos\WindowsFormsApp4\WindowsFormsApp4\Form1.cs 25 アクティブ

該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10//追加のライブラリ 11 12using System.IO; 13 14namespace WindowsFormsApp4 15{ 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 //初期化 25 comboBox1.Items.Clear(); 26 27 //項目の追加 28 comboBox1.Items.Add("処理なし"); 29 comboBox1.Items.Add("平滑化"); 30 comboBox1.Items.Add("鮮鋭化"); 31 comboBox1.Items.Add("エッジ検出"); 32 comboBox1.Items.Add("二値化"); 33 34 //初期選択を"処理なし"に設定 35 comboBox1.SelectedIndex = 0; 36 } 37 38 //読み込み処理 39 private void button1_Click(object sender, EventArgs e) 40 { 41 if (openFileDialog1.ShowDialog() == DialogResult.OK) 42 { 43 Bitmap bmp = new Bitmap(openFileDialog1.FileName); 44 pictureBox1.Image = bmp; 45 46 } 47 } 48 49 //保存 50 private void button2_Click(object sender, EventArgs e) 51 { 52 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 53 { 54 pictureBox1.Image.Save(saveFileDialog1.FileName); 55 } 56 } 57 58 //処理開始 59 private void button3_Click(object sender, EventArgs e) 60 { 61 Bitmap origin = new Bitmap(pictureBox1.Image); 62 63 //画像サイズの取得 64 int nx = origin.Width; 65 int ny = origin.Height; 66 67 double[,] data = new double[origin.Width, origin.Height]; 68 double[,] Result = new double[origin.Width, origin.Height]; 69 Bitmap dst = new Bitmap(nx, ny); 70 71 //ピクセルデータの取得 72 for (int i = 0; i < nx; i++) 73 { 74 for (int j = 0; j < ny; j++) 75 { 76 data[i, j] = (double)((origin.GetPixel(i, j).R + origin.GetPixel(i, j).G + origin.GetPixel(i, j).B) / 3); 77 } 78 } 79 80 switch (comboBox1.SelectedItem.ToString()) 81 { 82 case "処理なし": 83 pictureBox2.Image = pictureBox1.Image; 84 break; 85 86 case "平滑化": 87 Result = smoothing(data); 88 dst = gray_level_transformation(Result, nx, ny); 89 pictureBox2.Image = dst; 90 break; 91 92 case "鮮鋭化": 93 Result = sharpening(data); 94 dst = gray_level_transformation(Result, nx, ny); 95 pictureBox2.Image = dst; 96 break; 97 98 case "エッジ検出": 99 Result = edge_detectioning(data); 100 dst = gray_level_transformation(Result, nx, ny); 101 pictureBox2.Image = dst; 102 break; 103 104 case "二値化": 105 //閾値 106 int n = int.Parse(textBox1.Text); 107 Result = thresholding(data, n); 108 dst = gray_level_transformation(Result, nx, ny); 109 pictureBox2.Image = dst; 110 break; 111 } 112 113 } 114 115 //処理結果保存 116 private void button4_Click(object sender, EventArgs e) 117 { 118 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 119 { 120 string extension = Path.GetExtension(saveFileDialog1.FileName); 121 122 pictureBox2.Image.Save(saveFileDialog1.FileName); 123 } 124 } 125 126 //平滑化処理 127 private double[,] smoothing(double[,] data) 128 { 129 int xsize, ysize; 130 double[,] c = { 131 { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0}, 132 { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0}, 133 { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0} 134 }; 135 136 xsize = data.GetLength(0); 137 ysize = data.GetLength(1); 138 139 double[,] Result = new double[xsize, ysize]; 140 141 for (int j = 0; j < ysize; j++) 142 for (int i = 0; i < xsize; i++) 143 Result[i, j] = 0.0; 144 for (int j = 1; j < ysize - 1; j++) 145 for (int i = 1; i < xsize - 1; i++) 146 { 147 for (int k = 0; k < 3; k++) 148 for (int l = 0; l < 3; l++) 149 Result[i, j] += (double)data[i + l - 1, j + k - 1] * c[l, k]; 150 } 151 152 return Result; 153 } 154 155 //鮮鋭化処理 156 private double[,] sharpening(double[,] data) 157 { 158 int xsize, ysize; 159 double[,] c = { 160 { 0.0, -1.0, 0.0}, 161 { -1.0, 5.0, -1.0}, 162 { 0.0, -1.0, 0.0} 163 }; 164 165 xsize = data.GetLength(0); 166 ysize = data.GetLength(1); 167 168 double[,] Result = new double[xsize, ysize]; 169 170 for (int j = 0; j < ysize; j++) 171 for (int i = 0; i < xsize; i++) 172 Result[i, j] = 0.0; 173 for (int j = 1; j < ysize - 1; j++) 174 for (int i = 1; i < xsize - 1; i++) 175 { 176 for (int k = 0; k < 3; k++) 177 for (int l = 0; l < 3; l++) 178 Result[i, j] += (double)data[i + l - 1, j + k - 1] * c[l, k]; 179 } 180 181 return Result; 182 } 183 184 //エッジの検出 185 private double[,] edge_detectioning(double[,] data) 186 { 187 int xsize, ysize; 188 double[,] c = { 189 { 0.0, 1.0, 0.0}, 190 { 1.0, -4.0, 1.0}, 191 { 0.0, 1.0, 0.0} 192 }; 193 194 xsize = data.GetLength(0); 195 ysize = data.GetLength(1); 196 197 double[,] Result = new double[xsize, ysize]; 198 199 for (int j = 0; j < ysize; j++) 200 for (int i = 0; i < xsize; i++) 201 Result[i, j] = 0.0; 202 for (int j = 1; j < ysize - 1; j++) 203 for (int i = 1; i < xsize - 1; i++) 204 { 205 for (int k = 0; k < 3; k++) 206 for (int l = 0; l < 3; l++) 207 Result[i, j] += (double)data[i + l - 1, j + k - 1] * c[l, k]; 208 } 209 210 return Result; 211 } 212 213 //2値化処理 214 private double[,] thresholding(double[,] data, int th) 215 { 216 217 int xsize, ysize; 218 219 xsize = data.GetLength(0); 220 ysize = data.GetLength(1); 221 222 223 double[,] Result = new double[xsize, ysize]; 224 225 for (int j = 0; j < ysize; j++) 226 for (int i = 0; i < xsize; i++) 227 Result[i, j] = 0.0; 228 229 for (int j = 0; j < ysize; j++) 230 for (int i = 0; i < xsize; i++) 231 { 232 233 if (data[i, j] > th) 234 { 235 Result[i, j] = 255; 236 237 } 238 else 239 { 240 Result[i, j] = 0; 241 } 242 } 243 return Result; 244 245 } 246 247 // 248 private Bitmap gray_level_transformation(double[,] Result, int nx, int ny) 249 { 250 double max = double.MinValue; 251 double min = double.MaxValue; 252 byte[,] img = new byte[nx, ny]; 253 254 for (int i = 0; i < nx; i++) 255 for (int j = 0; j < ny; j++) 256 { 257 if (max < Result[i, j]) 258 max = Result[i, j]; 259 260 if (min > Result[i, j]) 261 min = Result[i, j]; 262 } 263 Bitmap bmp = new Bitmap(nx, ny); 264 Color c_c; 265 for (int i = 0; i < nx; i++) 266 for (int j = 0; j < ny; j++) 267 { 268 img[i, j] = (byte)(255 * (Result[i, j] - min) / (max - min)); 269 c_c = Color.FromArgb(img[i, j], img[i, j], img[i, j]); 270 bmp.SetPixel(i, j, c_c); 271 } 272 return bmp; 273 } 274 } 275 } 276

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/04/17 15:11

エラーメッセージをそのままコピペで提示しましょう
great-degin

2020/04/17 15:22

ご指摘ありがとうございます。追記いたしました。
guest

回答2

0

配られたお手本のソースコードに描いてある通りに作成したはずなんですが、

そのお手本にどういう手順が書かれていたのかわかりませんが、本当に書かれているとおりに作業してエラーになるのだとしたら、そのお手本が説明すべき事を説明できていないんでしょう。

そもそもWinFormsの開発の方法がわからないという事なら、公式のチュートリアルからやった方が良いんじゃないでしょうか。

https://docs.microsoft.com/ja-jp/visualstudio/ide/tutorial-1-create-a-picture-viewer?view=vs-2019

投稿2020/04/17 16:12

編集2020/04/17 16:14
gentaro

総合スコア8949

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

WindowsFormのコードは、コードをコピペするだけではダメです

デザイン画面でそれぞれのコンポーネントをフォーム上に配置してやらないとダメです

投稿2020/04/17 15:14

y_waiwai

総合スコア87749

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問