Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SerializeAndDeserializeImage
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnBrowse_Click(object sender, EventArgs e)
- {
- var dlg = new OpenFileDialog();
- dlg.Title = "Open Image";
- dlg.Filter = "jpg files (*.jpg)|*.jpg";
- dlg.Multiselect = true;
- if(dlg.ShowDialog() == DialogResult.OK)
- {
- const int margin = 20;
- int x = margin;
- int y = 4;
- foreach (var item in dlg.FileNames)
- {
- PictureBox pic = new PictureBox();
- pic.Location = new Point(x, y);
- pic.SizeMode = PictureBoxSizeMode.AutoSize;
- pic.Load(item);
- pic.BorderStyle = BorderStyle.Fixed3D;
- pic.Parent = flowLayoutPanel1;
- x += pic.Width;
- }
- }
- }
- private void btnsave_Click(object sender, EventArgs e)
- {
- if (sfdSerialization.ShowDialog() == DialogResult.OK)
- {
- List<Image> input_images = new List<Image>();
- foreach (PictureBox pic in flowLayoutPanel1.Controls)
- {
- input_images.Add((Bitmap)pic.Image);
- }
- using (FileStream fs = new FileStream(
- sfdSerialization.FileName, FileMode.Create))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(fs, input_images);
- }
- }
- }
- private void btn_loadimage_Click(object sender, EventArgs e)
- {
- if (ofdSerialization.ShowDialog() == DialogResult.OK)
- {
- using (FileStream fs = new FileStream(
- ofdSerialization.FileName, FileMode.Open))
- {
- BinaryFormatter formattter = new BinaryFormatter();
- List<Image> output_images =
- (List<Image>)formattter.Deserialize(fs);
- const int margin = 10;
- int x = margin;
- int y = 10;
- foreach (Image image in output_images)
- {
- PictureBox pic = new PictureBox();
- pic.Location = new Point(x, y);
- pic.SizeMode = PictureBoxSizeMode.AutoSize;
- pic.Image = image;
- pic.BorderStyle = BorderStyle.Fixed3D;
- pic.Parent = flowLayoutPanel1;
- x += pic.Width;
- }
- }
- }
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- flowLayoutPanel1.Controls.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement