Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户可从设置文件中选择背景图像(物理和资源)_C#_String_Image_Background_Resources - Fatal编程技术网

C# 用户可从设置文件中选择背景图像(物理和资源)

C# 用户可从设置文件中选择背景图像(物理和资源),c#,string,image,background,resources,C#,String,Image,Background,Resources,我有一个WinForms应用程序,它带有一个单选按钮,可以更改表单的背景图像,并将该背景图像的位置保存到文本文件中,这样用户每次运行应用程序时都会加载所选的背景 现在,这些背景图像需要位于用户的/USERNAME/My Documents/Application Name/Skins/Default/文件夹中 我希望申请表能做到以下几点: 用户可以选择位于/USERNAME/My Documents/Application Name/Skins/Custom/文件夹中的自定义外观 还可以从默认外

我有一个WinForms应用程序,它带有一个单选按钮,可以更改表单的背景图像,并将该背景图像的位置保存到文本文件中,这样用户每次运行应用程序时都会加载所选的背景

现在,这些背景图像需要位于用户的/USERNAME/My Documents/Application Name/Skins/Default/文件夹中

我希望申请表能做到以下几点:

用户可以选择位于/USERNAME/My Documents/Application Name/Skins/Custom/文件夹中的自定义外观

还可以从默认外观中进行选择,但这些外观位于项目的资源中。我这里的问题是:我可以从设置文件中读取和写入自定义皮肤的物理位置,但我不确定如何使用此方法添加从内置项目资源位置选择背景的功能,并且仍然将其保存到设置文件中

下面是我现在用于物理位置的代码:

    private void DefaultThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application", "Skins", "Default", "DefaultBackground.png");
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;
我希望能够完成以下任务:

// USER SELECTS DEFAULT BACKGROUND
    private void DefaultThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = ((System.Drawing.Image)(Properties.Resources.DefaultBackground))  // THIS IS THE PART I'M STUCK ON
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;

// USER SELECTS CUSTOM BACKGROUND

    private void CustomThemeButton_CheckedChanged(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string currentBackgroundImage = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application", "Skins", "Custom", "Background.png");
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (currentBackgroundImage);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First()); ;
有没有一种方法可以像读取物理位置一样读取资源位置,而不必将文件实际放在物理文件夹中

this.Control.BackgroundImage = Project.Properties.Resources.resourceName;


因此,有了它,你就不需要将路径保存到任何地方,因为你已经在资源中有了路径。这就是为什么我认为不需要保存路径的原因。你所需要做的就是告诉你的应用程序通过文本文件加载一个资源或外部文件。

我已经找到了一个解决问题的方法更改如何选择我的背景。在“我的主题选择”窗体上,用户选择的主题将作为包含主题名称的简单字符串写入my Settings.cfg文件

然后从资源或主题的物理位置加载主题,具体取决于选择的主题

主题选择表格:

    private void DEFAULTTHEME_Click(object sender, EventArgs e)
    {            
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");            
        string SelectedTheme = "Default Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE           
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Properties.Resources.DefaultBackground;
    }

    private void LIGHTTHEME_Click(object sender, EventArgs e)
    {            
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");            
        string SelectedTheme = "Light Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE           
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Properties.Resources.LightBackground;
    }

    private void CUSTOMTHEME_Click(object sender, EventArgs e)
    {
        string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
        string CustomBackground = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Custom", "Background.png"); //THIS IS SO MY APPLICATION KNOWS THE LOCATION OF THE CUSTOM BACKGROUND IMAGE
        string SelectedTheme = "Custom Theme"; //THIS GETS WRITTEN TO THE SETTINGS FILE
        var lines = System.IO.File.ReadAllLines(SettingsPath);
        lines[7] = (SelectedTheme);
        System.IO.File.WriteAllLines(SettingsPath, lines);
        MainForm.BackgroundImage = Image.FromFile(CustomBackground);
    }
当我的主窗体加载时,它会检查第8行设置文件的内容,并将其转换为一个字符串,即所选主题的名称。然后,表单根据字符串的内容决定要显示的背景图像:

private void Form1_Load(object sender, EventArgs e)   
{
 string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
 string CustomBackground = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Custom", "Background.png");
 string SelectedTheme = System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First();

//LOAD THEME
        if (SelectedTheme.Equals("Default Theme"))
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
        else if (SelectedTheme.Equals("Light Theme"))
        {
            this.BackgroundImage = Properties.Resources.LightBackground;
        }
        else if (SelectedTheme.Equals("Custom Theme"))
            this.BackgroundImage = Image.FromFile(CustomBackground);
        else
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
}

我不确定这是否是实现这一点的理想方法,因为我对C非常陌生,但这种方法对我的应用程序非常有效。

只是一个问题。。。为什么不让客户端浏览该文件,然后在应用程序文件夹中复制一份?这样你就可以检查文件并每次都将其设置为背景..这几乎是它已经在做的事情,但我希望能够做的是,让我的应用程序的默认主题基于项目资源,并让自定义主题基于物理文件夹。这样,用户就可以随心所欲地编辑自定义主题,但如果他们想/搞糟一些事情,仍然可以返回默认主题;因为他们无权更改默认主题,所以假设您已经知道如何将文件添加到资源中,但您不知道如何从资源中读取?或者我没有再次获得它pMy默认主题将已内置到项目资源中,我需要一种方法,让我的应用程序将该资源名称/位置或任何你想调用的名称/位置转换为字符串,并将该字符串写入我的设置文件,以便每次加载应用程序时都可以调用该位置。我已经能够处理用户“我的文档”文件夹中物理文件夹中的图像,但我不知道如何将内置资源的位置保存到文本文件中。我希望这有助于解释我的问题。我不明白你为什么要将资源的位置保存到文本文件中,但要访问它,你所需要做的就是强制转换,然后从属性中访问它。资源…能够访问它,你可以在用户想要切换回默认值时应用它,我开始使用文本文件是一个错误,但这是你的决定。如果访问是一个问题,我可以写一个关于如何通过资源访问它的示例。我希望这能有所帮助谢谢,但我已经知道如何从资源设置控件背景图像,我的问题是如何将该资源转换为字符串,但我现在已经弄明白了。
private void Form1_Load(object sender, EventArgs e)   
{
 string SettingsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Settings.cfg");
 string CustomBackground = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Custom", "Background.png");
 string SelectedTheme = System.IO.File.ReadLines(SettingsPath).Skip(7).Take(1).First();

//LOAD THEME
        if (SelectedTheme.Equals("Default Theme"))
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
        else if (SelectedTheme.Equals("Light Theme"))
        {
            this.BackgroundImage = Properties.Resources.LightBackground;
        }
        else if (SelectedTheme.Equals("Custom Theme"))
            this.BackgroundImage = Image.FromFile(CustomBackground);
        else
        {
            this.BackgroundImage = Properties.Resources.DefaultBackground;
        }
}