【问题标题】:How to get image file name in a textbox?如何在文本框中获取图像文件名?
【发布时间】:2012-03-10 14:03:28
【问题描述】:

我对 C# 文件名有疑问。我用 PictureBox 展示了一些图片。我还想在文本框中写下图片的名称。我搜索了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[..];

我想在文本框中写棒球棒、新娘、手镯等。我能做些什么?有优惠吗?

【问题讨论】:

  • 你的图片都是来自Properties.Resources还是只是一个例子?
  • 这是一个小例子。如果有其他选择我可以试试。我只是写图片的名字..
  • 正如我在回答中所说,您可以将名称和图像保存在一起,这样当您检索一个元素时,您会同时拥有名称和图像。但也许您应该指定图像的来源,以了解这样的方法对您是否可行。

标签: c# file directory fileinfo imagelist


【解决方案1】:

嗯,最简单的方法之一是将名称和图像保存在 List&lt;KeyValuePair&lt;string,Image&gt;&gt;IDictionary&lt;string,image&gt; 中。

这是一个使用IDictionary&lt;string,image&gt;的示例
(由于索引,我决定使用SortedList&lt;&gt;):

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&lt;KeyValuePair&lt;string,Image&gt;&gt; 将是:

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;

【讨论】:

  • 好的。我使用 List> 并写了 images.Add("bracelet", Properties.Resources.bracelet);哪里错了?我认为我们关闭了解决方案。
  • @physee:使用 kvpair 列表与字典不同。我编辑了问题以使其清楚。请注意Add(...) 方法和访问[...] 的区别
  • 我做到了。 SortedList images = new SortedList();非常感谢...
  • 但是我有一点问题。当我运行代码时,单击 2 次后,“已存在具有相同键的条目”。说过。有什么问题? ://
  • @physee:您在SortedList&lt;&gt; 中多次添加相同的图像。这种结构(像所有字典一样)只允许同一个键有一个条目。如果需要添加多张同名图片,请切换到List&lt;&gt;
【解决方案2】:

这个功能已经内置了...

图像列表以一种您可以通过名称引用它们并返回图像的方式存储它们的图像。

一次性使用:

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

这将返回所传递索引处的图像名称

为以后存储值:

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

这将返回一个字典,该字典将图片索引引用到图像列表中的字符串名称。

再次内置,无需尝试扩展功能。给它...

【讨论】:

    【解决方案3】:

    您可以使用反射获取所有资源及其键(资源名称):

    //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
            ...
    

    更新:我已更新代码以将值保存在字典中,以便您以后方便地使用它们。

    【讨论】:

    • 使用我发布的代码,您可以获取图像及其名称,因此在设置图像时,您也可以设置其名称。
    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多