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

时间:2016-05-22 09:56:36

标签: c# visual-studio-2010

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

1 个答案:

答案 0 :(得分:2)

使用保存方法保存设置

Properties.Settings.Default.Save();

如果要将图像添加到设置:

使用类型字符串添加新设置并使用如下:

将图像保存到设置(关闭表单时)

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

如何添加自定义设置?

http://www.codeproject.com/Articles/29130/Windows-Forms-Creating-and-Persisting-Custom-User