对象串行化

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7// 添加新的命名空间。
  8using System.Runtime.Serialization;
  9using System.Runtime.Serialization.Formatters.Binary;
 10using System.IO;
 11
 12namespace 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(164344);
 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(72313);
 83            this.textBox1.Name = "textBox1";
 84            this.textBox1.Size = new System.Drawing.Size(32025);
 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(402304);
 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(274344);
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(8320);
110            this.label1.Name = "label1";
111            this.label1.Size = new System.Drawing.Size(6018);
112            this.label1.TabIndex = 4;
113            this.label1.Text = "文件名:";
114            // 
115            // button3
116            // 
117            this.button3.Location = new System.Drawing.Point(54344);
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(818);
136            this.ClientSize = new System.Drawing.Size(402370);
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

转载于:https://www.cnblogs.com/gofficer/archive/2007/08/24/867898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值