Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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中保存窗体关闭时的背景图像#_C#_Visual Studio 2010 - Fatal编程技术网

C# 如何在C中保存窗体关闭时的背景图像#

C# 如何在C中保存窗体关闭时的背景图像#,c#,visual-studio-2010,C#,Visual Studio 2010,我正在用c#制作一个桌面应用程序,当用户从菜单条中选择特定的背景名称时,背景将变成所需的背景。问题是我无法保存用户输入,我尝试了设置,但在设置中找不到“system.drawing.image”,所以有没有办法保存用户更改的背景?不允许用户更改外部背景,只允许更改资源文件夹中的背景。这是我的代码,显示system.drawing.color不能代替drawing.image的错误 using System; using System.Collections.Generic;

我正在用c#制作一个桌面应用程序,当用户从菜单条中选择特定的背景名称时,背景将变成所需的背景。问题是我无法保存用户输入,我尝试了设置,但在设置中找不到“system.drawing.image”,所以有没有办法保存用户更改的背景?不允许用户更改外部背景,只允许更改资源文件夹中的背景。这是我的代码,显示system.drawing.color不能代替drawing.image的错误

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace TAC
    {
        public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.Location = new Point(165, 157);
        panel2.Location = new Point(289, 158);
        panel3.Location = new Point(47, 275);
        panel4.Location = new Point(47, 402);
        this.BackgroundImage = Properties.Settings.Default.FormImage;
    }

    private void bLUEToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = TAC.Properties.Resources.tex1;
    }

    private void gREENToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = TAC.Properties.Resources.tex2;
    }

    private void oRANGEToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = TAC.Properties.Resources.tex3;
    }

    private void rEDToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = TAC.Properties.Resources.tex4;
    }

    private void pURPLEToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = TAC.Properties.Resources.tex5;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.FormImage = this.BackgroundImage;
    }
   }
  }

使用“保存”方法保存设置

Properties.Settings.Default.Save();
如果要将图像添加到设置中:

添加类型为string的新设置,并按如下方式使用:

将图像保存到设置(关闭窗体时)

从设置中读取图像并设置为背景(在表单加载中)

如何添加自定义设置


我在表单关闭时添加了您在此处给出的保存代码,并在表单加载时添加了使用代码。首先,当我尝试调试我的应用程序时,分号给出了异常错误。其次,我是否应该对三个不同的图像使用此方法三次?就像我添加了一个下拉列表,用户从中选择图像,所以当选择图像时,表单关闭时,它应该被保存,当表单重新打开时,所选图像应该是背景。请在这里下载并编辑它们,以便我能理解该做什么。由于英语原因,我不知道您说了什么。您可以将代码放入方法中以供重用:SaveToSetting(图像i)。您可以将用户选择的每个图像设置为背景。然后,当你想关闭时,将其保存到设置。以表单加载读取设置我无法下载你的项目。但我编辑了我的答案。只需将所有内容放在我说的地方,作为noob,我不明白你说的话。请看我设置发送给你的文件!并编辑它们,以便我能理解!
MemoryStream ms = new MemoryStream();
Propertis.Resources.MyImage.Save(ms,ImageFormat.Jpeg);
Properties.Settings.Default.BackImg = Convert.ToBase64String(ms.ToArray());
Properties.Settings.Default.Save();
string img =  Properties.Settings.Default.BackImg ;
byte[] i = Convert.FromBase64String(img);
this.BackgroundImage = Image.FromStream(new MemoryStream(i));