对象串行化
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
// 添加新的命名空间。
8
using System.Runtime.Serialization;
9
using System.Runtime.Serialization.Formatters.Binary;
10
using System.IO;
11![]()
12
namespace Serialization
13
{
14
/// <summary>
15
/// 对象串行化。
16
/// </summary>
17
public class Form1 : System.Windows.Forms.Form
18
{
19
private System.Windows.Forms.Button button1;
20
private System.Windows.Forms.TextBox textBox1;
21
private System.Windows.Forms.PictureBox pictureBox1;
22
private System.Windows.Forms.Button button2;
23
private System.Windows.Forms.Label label1;
24
private System.Windows.Forms.Button button3;
25
private System.Windows.Forms.OpenFileDialog openFileDialog1;
26
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
27
/// <summary>
28
/// 必需的设计器变量。
29
/// </summary>
30
private System.ComponentModel.Container components = null;
31![]()
32
public Form1()
33
{
34
// Windows 窗体设计器支持所必需的
35
InitializeComponent();
36
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
37
}
38![]()
39
/// <summary>
40
/// 清理所有正在使用的资源。
41
/// </summary>
42
protected override void Dispose( bool disposing )
43
{
44
if( disposing )
45
{
46
if (components != null)
47
{
48
components.Dispose();
49
}
50
}
51
base.Dispose( disposing );
52
}
53![]()
54
Windows Form Designer generated code
153![]()
154
/// <summary>
155
/// 应用程序的主入口点。
156
/// </summary>
157
[STAThread]
158
static void Main()
159
{
160
Application.Run(new Form1());
161
}
162
// 定义私有变量。
163
// 输出文件流对象。
164
public Stream s;
165
// 串行化对象。
166
public BinaryFormatter f;
167
// 串行化对象输出。
168
private void button1_Click(object sender, System.EventArgs e)
169
{
170
if(pictureBox1.Image != null)
171
{
172
saveFileDialog1.Filter = "自定义文件(*.ser)|*.ser";
173
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
174
{
175
s = File.Create(saveFileDialog1.FileName);
176
f = new BinaryFormatter();
177
// 存储图形文件和对应的文件名。
178
f.Serialize(s, pictureBox1.Image);
179
f.Serialize(s, textBox1.Text);
180
s.Close();
181
}
182
}
183
}
184
// 串行化对象输入。
185
private void button2_Click(object sender, System.EventArgs e)
186
{
187
openFileDialog1.Filter = "自定义文件(*.ser)|*.ser";
188
if(openFileDialog1.ShowDialog() == DialogResult.OK)
189
{
190
s = File.OpenRead(openFileDialog1.FileName);
191
f = new BinaryFormatter();
192
pictureBox1.Image = (Image)f.Deserialize(s);
193
textBox1.Text = (string)f.Deserialize(s);
194
s.Close();
195
}
196
}
197
// 选择要存储的图形文件。
198
private void button3_Click(object sender, System.EventArgs e)
199
{
200
openFileDialog1.Filter = "图形文件(*.bmp;*.jpg;*.jpeg;*.gif)|*.bmp;*.jpg;*.jpeg;*.gif";
201
if(openFileDialog1.ShowDialog() == DialogResult.OK)
202
{
203
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
204
textBox1.Text = openFileDialog1.FileName;
205
}
206
}
207
}
208
}
209![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7
// 添加新的命名空间。8
using System.Runtime.Serialization;9
using System.Runtime.Serialization.Formatters.Binary;10
using System.IO;11

12
namespace Serialization13
{14
/// <summary>15
/// 对象串行化。16
/// </summary>17
public class Form1 : System.Windows.Forms.Form18
{19
private System.Windows.Forms.Button button1;20
private System.Windows.Forms.TextBox textBox1;21
private System.Windows.Forms.PictureBox pictureBox1;22
private System.Windows.Forms.Button button2;23
private System.Windows.Forms.Label label1;24
private System.Windows.Forms.Button button3;25
private System.Windows.Forms.OpenFileDialog openFileDialog1;26
private System.Windows.Forms.SaveFileDialog saveFileDialog1;27
/// <summary>28
/// 必需的设计器变量。29
/// </summary>30
private System.ComponentModel.Container components = null;31

32
public Form1()33
{34
// Windows 窗体设计器支持所必需的35
InitializeComponent();36
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码37
}38

39
/// <summary>40
/// 清理所有正在使用的资源。41
/// </summary>42
protected override void Dispose( bool disposing )43
{44
if( disposing )45
{46
if (components != null) 47
{48
components.Dispose();49
}50
}51
base.Dispose( disposing );52
}53

54
Windows Form Designer generated code153

154
/// <summary>155
/// 应用程序的主入口点。156
/// </summary>157
[STAThread]158
static void Main() 159
{160
Application.Run(new Form1());161
}162
// 定义私有变量。163
// 输出文件流对象。164
public Stream s;165
// 串行化对象。166
public BinaryFormatter f;167
// 串行化对象输出。168
private void button1_Click(object sender, System.EventArgs e)169
{170
if(pictureBox1.Image != null)171
{172
saveFileDialog1.Filter = "自定义文件(*.ser)|*.ser";173
if(saveFileDialog1.ShowDialog() == DialogResult.OK)174
{175
s = File.Create(saveFileDialog1.FileName);176
f = new BinaryFormatter();177
// 存储图形文件和对应的文件名。178
f.Serialize(s, pictureBox1.Image);179
f.Serialize(s, textBox1.Text);180
s.Close();181
}182
}183
}184
// 串行化对象输入。185
private void button2_Click(object sender, System.EventArgs e)186
{187
openFileDialog1.Filter = "自定义文件(*.ser)|*.ser";188
if(openFileDialog1.ShowDialog() == DialogResult.OK)189
{190
s = File.OpenRead(openFileDialog1.FileName);191
f = new BinaryFormatter();192
pictureBox1.Image = (Image)f.Deserialize(s);193
textBox1.Text = (string)f.Deserialize(s);194
s.Close();195
}196
}197
// 选择要存储的图形文件。198
private void button3_Click(object sender, System.EventArgs e)199
{200
openFileDialog1.Filter = "图形文件(*.bmp;*.jpg;*.jpeg;*.gif)|*.bmp;*.jpg;*.jpeg;*.gif";201
if(openFileDialog1.ShowDialog() == DialogResult.OK)202
{203
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);204
textBox1.Text = openFileDialog1.FileName;205
}206
}207
}208
}209



浙公网安备 33010602011771号