简体   繁体   中英

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. 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 ? 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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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