如何使Image.FromFile使用我的应用程序文件夹?

时间:2016-11-20 19:18:06

标签: c# visual-studio

现在我正在制作一个程序,根据组合框选择更改图片框中的图片。它应该如何工作,但如果我想导出它并将其发送给朋友,它将无法工作,因为我使用它的唯一方法是使用确切的文件名(包括我的用户目录)。尽管所有资源都在Resource文件夹中,但如果我只给出文件名,则会给出FileNotFoundException。

我对编程很陌生,因此非常感谢具体说明。

public Form1()
    {

        InitializeComponent();


        MapList();
    }

    public void MapList()
    {
        cBox.Items.Add(
            new Map() {Name= "Cache" ,
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_cache_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Cobblestone",
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_cbble_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Dust2",
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_dust2_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Inferno",
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_inferno_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Mirage",
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_mirage_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Nuke",
                Image = @"C:\Users\Pampperin\Desktop\Videos\Coding\Projects\CSGO Audio Help\MinimapSoundDisplay\MinimapSoundDisplay\Resources\de_nuke_radar.png"
            });
        cBox.Items.Add(
            new Map()
            {
                Name = "Overpass",
                Image = "Resources.de_overpass_radar.png"
            });
    }


    private void cBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cBox.SelectedIndex > -1)
        {
            var imageName = ((Map)cBox.SelectedItem).Image;
            var file = System.IO.Path.Combine(Application.StartupPath, "Resources", imageName);
            pictureBox.Image = Image.FromFile(file);
        }
    }

和地图类

public class Map
{
    public string Name { get; set; }
    public string Image { get; set; }
    public override string ToString()
    {
        return this.Name;
    }
}

1 个答案:

答案 0 :(得分:0)

这为您提供了应用程序路径:

string apppath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

我没有测试,但是这应该可以工作(以防止你使用反射):

string apppath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

然后您应该可以设置文件名:

Image = System.IO.Path.Combine(apppath, "de_cache_radar.png");