简体   繁体   English

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

[英]How To Save Background Image On Form Close In C#

I am making a desktop app in c# in which when the user selects specific name of background from the menu strip, then the background shall turn to that required background. 我正在用C#开发一个桌面应用程序,其中当用户从菜单栏中选择特定的背景名称时,背景将变为所需的背景。 The problem is that i cannot save the user input, i tried settings but i cannot find "system.drawing.image" in settings so is there any way i can save the user changed background ? 问题是我无法保存用户输入,我尝试了设置,但是在设置中找不到“ system.drawing.image”,所以有什么办法可以保存用户更改的背景吗? No external backgrounds a user is allowed to change, just the ones in the resource folder. 不允许用户更改任何外部背景,仅允许更改资源文件夹中的背景。 Here is my code which shows error that system.drawing.color cannot take place of 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;
    }
   }
  }

Use Save method to save settings 使用保存方法保存设置

Properties.Settings.Default.Save();

If you want add an image to settings : 如果要在设置中添加图片:

Add new setting with type string and use like this: 使用字符串类型添加新设置,并按以下方式使用:

Save an image to setting ( when you close form) 将图像保存到设置中(当您关闭表单时)

MemoryStream ms = new MemoryStream();
Propertis.Resources.MyImage.Save(ms,ImageFormat.Jpeg);
Properties.Settings.Default.BackImg = Convert.ToBase64String(ms.ToArray());
Properties.Settings.Default.Save();

And read image from setting and set to background(in form load) 并从设置中读取图像并设置为背景(以表格加载)

string img =  Properties.Settings.Default.BackImg ;
byte[] i = Convert.FromBase64String(img);
this.BackgroundImage = Image.FromStream(new MemoryStream(i));

How add custom settings ? 如何添加自定义设置?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM