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[..];

そして、テキストボックスに野球バット、花嫁、ブレスレットなどを書きたいです。私に何ができる?何か申し出はありますか?

4

3 に答える 3

1

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

これは a を使用した例です (私はインデックス作成のためIDictionary<string,image>
に決めました) :SortedList<>

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];

の場合は次のList<KeyValuePair<string,Image>>ようになります。

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;
于 2012-03-10T14:43:53.483 に答える
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
        ...

更新:値を辞書に保存するようにコードを更新したので、後で便利に使用できます。

于 2012-03-10T14:35:10.513 に答える