如何在 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;
}
}
}
使用 Save 方法保存设置
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
我正在用 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;
}
}
}
使用 Save 方法保存设置
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