1.C#解决Bitmap导致的内存溢出问题
System.GC.Collect();//垃圾回收 清内存 不然图片一直增加内存
2.参数保存与加载
把所有要保存的参数放在一个类
修改参数后,把类对象序列化保存,在启动时反序列化到对象。
如果新增参数,类中新增即可,其他不变。
涉及类,序列化与与反序列化,保存,读取。序列化的格式文件人类不方便读与修改。
参数类
[Serializable]
public class Para {
public int height = 16;
public int width = 16;
public int xOffset = 0;
public int yOffset = 0;
public int dotSize = 4;
public int dotInteval = 1;
public Font myFont = new System.Drawing.Font(System.Drawing.SystemFonts.DefaultFont.Name, System.Drawing.SystemFonts.DefaultFont.Size);
[NonSerialized]
public string memo="小黄人软件"; //不参与序列化
}
封装的类及调用示例
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace WindowsFormsApplication1
{
//调用示例
//Para p = new Para();
//Object o = SerializePara.read();//读所有参数
//if (o != null) { p = (Para)o; }
//p.dotSize = 10; //改参数
//SerializePara.write(p); //保存所有参数
class SerializePara
{
public static string filePath = @"Para.obj";
public static Object read()
{
Object obj=null;
if (File.Exists(filePath))
{
IFormatter formatter = new BinaryFormatter();
Stream fs = File.OpenRead(filePath);
obj = formatter.Deserialize(fs);
fs.Dispose();
}
return obj;
}
public static void write(Object obj)
{
IFormatter formatter = new BinaryFormatter();
Stream fs = File.OpenWrite(filePath);
formatter.Serialize(fs, obj);
fs.Dispose();
}
}
}
其它语言也类似,比如C、C++语言用结构体:
#include <iostream>
typedef struct Test
{
char name[2];
int old;
}Test;
int main()
{
Test t;
t.old = 1;
char temp[100] = { 0 };
memcpy(temp, (char *)&t, sizeof(t)); //保存 ,是取地址后再转成char*
t.old = 8;
memcpy( (char *)&t, temp, sizeof(t));//恢复
std::cout <<" old="<<t.old<<"\n"; //old=1 不是8
getchar();
return 0;
}