簡體   English   中英

如何在C#中關閉窗體上保存背景圖像

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

我正在用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();

如果要在設置中添加圖片:

使用字符串類型添加新設置,並按以下方式使用:

將圖像保存到設置中(當您關閉表單時)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM