2012-03-10 10 views
1

C#ファイル名に問題があります。私はPictureBoxでいくつかの写真を表示します。また、TextBoxに画像の名前を書きたいと思います。私はfileinfo、directoryinfoを検索しますが、それは動作しません。テキストボックスで画像ファイル名を取得する方法は?

List<Image> images = new List<Image>(); 
images.Add(Properties.Resources.baseball_bat); 
images.Add(Properties.Resources.bracelet); 
images.Add(Properties.Resources.bride); 

pictureBox1.Image = images[..]; 

と私はTextBoxにbaseball_bat、bride、braceletなどを書きたいと思います。私に何ができる?どんなオファーですか?

+0

あなたの画像はすべて「Properties.Resources'からですか?それは単なる例ですか? – digEmAll

+0

これはちょっとした例です。もし私がそれを試すことができる代替案があれば。私はちょうど写真の名前を書く。 – nevra

+0

私は自分の答えで言ったように、あなたは名前と画像の両方を保存することができます。しかしおそらく、あなたがこのようなアプローチが可能かどうかを理解するために、画像のソースを指定するべきでしょう。 – digEmAll

答えて

0

この機能がすでに組み込まれています。 ..

画像リストには、画像を参照できる方法で保存されます。名前を付けて画像を返します。

シングル使用:

private Dictionary<int, string> GetImageNames(ImageList imglist) 
    { 
     var dict = new Dictionary<int, string>(); 
     int salt = 0; 

     foreach (var item in imglist.Images.Keys) 
     { 
      dict.Add(salt, item.ToString()); 
      salt++; 
     } 
     return dict; 
    } 

これは、インデックスを参照する辞書を返します。

private string GetImageName(ImageList imglist, int index) 
    { 
     return imglist.Images.Keys[index].ToString(); 
    } 

これは、渡されたインデックス後で用

ストア値で画像の名前を返します。の画像を画像リストの文字列名に変換します。

これもまた組み込まれているので、functを拡張しようとする必要はありません。それに...

0

あなたは、リフレクションを使用して、キー(リソース名)と一緒にすべてのリソースを取得することができます:

//a helper dictionary if you want to save the images and their names for later use 
var namesAndImages = new Dictionary<String, Image>(); 

var resourcesSet = Properties.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true); 

     foreach (System.Collections.DictionaryEntry myResource in resourcesSet) 
     { 
      if (myResource.Value is Image) //is this resource is associated with an image 
      { 
       String resName = myResource.Key.ToString(); //get resource's name 
       Image resImage = myResource.Value as Image; //get the Image itself 

       namesAndImages.Add(resName, resImage); 
      } 
     } 

     //now you can use the values saved in the dictionary and easily get their names 
     ... 

更新:を私は、辞書に値を保存するためのコードを更新しました後でそれらを便利に使用することができます。

+0

OK。キーを取得するにはどうしたらいいですか?私は何をすべきか理解できません。 – nevra

+0

私が投稿したコードを使用すると、名前とともに画像を取得できるので、画像を設定するときに名前を設定することもできます。 –

1

最も簡単な方法の1つは、名前と画像の両方をList<KeyValuePair<string,Image>>またはIDictionary<string,image>に保存することです。ここで

IDictionary<string,image>
を使用した例です(私があるため、インデックスのSortedList<>に決定):List<KeyValuePair<string,Image>>

var images = new SortedList<string, Image>(); 
images.Add("baseball_bat", Properties.Resources.baseball_bat); 
images.Add("bracelet", Properties.Resources.bracelet); 
... 

// when you show the first image... 
pictureBox1.Image = images.Values[0]; 
textBox1.Text = images.Keys[0]; 

// when you show the nth image... 
pictureBox1.Image = images.Values[n]; 
textBox1.Text = images.Keys[n]; 

は次のようになります。

var images = new List<KeyValuePair<string, Image>>(); 
images.Add(new KeyValuePair<string,Image>("baseball_bat", Properties.Resources.baseball_bat)); 
images.Add(new KeyValuePair<string,Image>("bracelet", Properties.Resources.bracelet)); 
... 

// when you show the first image... 
pictureBox1.Image = images[0].Values; 
textBox1.Text = images[0].Keys; 

// when you show the nth image... 
pictureBox1.Image = images[n].Values; 
textBox1.Text = images[n].Keys; 
+0

OK。 List >を使用し、その文字を書きます。 images.Add( "bracelet"、Properties.Resources.bracelet); どこが間違っていますか?私は解決策を閉じると思います。 – nevra

+0

@physee:kvpairのリストを使うのは辞書とは異なります。私はそれを明確にするために質問を編集しました。 'Add(...)'メソッドと '[...] 'の違いに注意してください。 – digEmAll

+0

私はそれを行いました。 SortedList images = new SortedList <文字列、イメージ>(); ありがとうございました... – nevra

関連する問題