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#region Windows Form Designer generated code
55
/**//// <summary>
56
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
57
/// 此方法的内容。
58
/// </summary>
59
private void InitializeComponent()
60
{
61
this.button1 = new System.Windows.Forms.Button();
62
this.textBox1 = new System.Windows.Forms.TextBox();
63
this.pictureBox1 = new System.Windows.Forms.PictureBox();
64
this.button2 = new System.Windows.Forms.Button();
65
this.label1 = new System.Windows.Forms.Label();
66
this.button3 = new System.Windows.Forms.Button();
67
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
68
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
69
this.SuspendLayout();
70
//
71
// button1
72
//
73
this.button1.Location = new System.Drawing.Point(164, 344);
74
this.button1.Name = "button1";
75
this.button1.TabIndex = 0;
76
this.button1.Text = "输出";
77
this.button1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
78
this.button1.Click += new System.EventHandler(this.button1_Click);
79
//
80
// textBox1
81
//
82
this.textBox1.Location = new System.Drawing.Point(72, 313);
83
this.textBox1.Name = "textBox1";
84
this.textBox1.Size = new System.Drawing.Size(320, 25);
85
this.textBox1.TabIndex = 1;
86
this.textBox1.Text = "";
87
//
88
// pictureBox1
89
//
90
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
91
this.pictureBox1.Name = "pictureBox1";
92
this.pictureBox1.Size = new System.Drawing.Size(402, 304);
93
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
94
this.pictureBox1.TabIndex = 2;
95
this.pictureBox1.TabStop = false;
96
//
97
// button2
98
//
99
this.button2.Location = new System.Drawing.Point(274, 344);
100
this.button2.Name = "button2";
101
this.button2.TabIndex = 3;
102
this.button2.Text = "输入";
103
this.button2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
104
this.button2.Click += new System.EventHandler(this.button2_Click);
105
//
106
// label1
107
//
108
this.label1.AutoSize = true;
109
this.label1.Location = new System.Drawing.Point(8, 320);
110
this.label1.Name = "label1";
111
this.label1.Size = new System.Drawing.Size(60, 18);
112
this.label1.TabIndex = 4;
113
this.label1.Text = "文件名:";
114
//
115
// button3
116
//
117
this.button3.Location = new System.Drawing.Point(54, 344);
118
this.button3.Name = "button3";
119
this.button3.TabIndex = 5;
120
this.button3.Text = "选择图片";
121
this.button3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
122
this.button3.Click += new System.EventHandler(this.button3_Click);
123
//
124
// openFileDialog1
125
//
126
this.openFileDialog1.Filter = "图像文件 (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|自定义文件(*.ser)|*.ser";
127
//
128
// saveFileDialog1
129
//
130
this.saveFileDialog1.FileName = "doc1";
131
this.saveFileDialog1.Filter = "自定义文件(*.ser)|*.ser";
132
//
133
// Form1
134
//
135
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
136
this.ClientSize = new System.Drawing.Size(402, 370);
137
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
138
this.button3,
139
this.label1,
140
this.button2,
141
this.pictureBox1,
142
this.textBox1,
143
this.button1});
144
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
145
this.MaximizeBox = false;
146
this.Name = "Form1";
147
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
148
this.Text = "对象串行化";
149
this.ResumeLayout(false);
150
151
}
152
#endregion
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

2

3

4

5

6

7

8

9

10

11

12

13



14


15

16

17

18



19

20

21

22

23

24

25

26

27


28

29

30

31

32

33



34

35

36

37

38

39


40

41

42

43



44

45



46

47



48

49

50

51

52

53

54


55


56

57

58

59

60



61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137



138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154


155

156

157

158

159



160

161

162

163

164

165

166

167

168

169



170

171



172

173

174



175

176

177

178

179

180

181

182

183

184

185

186



187

188

189



190

191

192

193

194

195

196

197

198

199



200

201

202



203

204

205

206

207

208

209
