Advertisement
hungvb

[C#] Serialize And Deserialize Image to Binary File

Oct 3rd, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace SerializeAndDeserializeImage
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void btnBrowse_Click(object sender, EventArgs e)
  23.         {
  24.             var dlg = new OpenFileDialog();
  25.             dlg.Title = "Open Image";
  26.             dlg.Filter = "jpg files (*.jpg)|*.jpg";
  27.             dlg.Multiselect = true;
  28.             if(dlg.ShowDialog() == DialogResult.OK)
  29.             {
  30.                 const int margin = 20;
  31.                 int x = margin;
  32.                 int y = 4;
  33.                 foreach (var item in dlg.FileNames)
  34.                 {
  35.                    
  36.                     PictureBox pic = new PictureBox();
  37.                     pic.Location = new Point(x, y);
  38.                     pic.SizeMode = PictureBoxSizeMode.AutoSize;
  39.                     pic.Load(item);
  40.                     pic.BorderStyle = BorderStyle.Fixed3D;
  41.                     pic.Parent = flowLayoutPanel1;
  42.                     x += pic.Width;
  43.                 }
  44.                
  45.             }
  46.         }
  47.  
  48.      
  49.         private void btnsave_Click(object sender, EventArgs e)
  50.         {
  51.             if (sfdSerialization.ShowDialog() == DialogResult.OK)
  52.             {      
  53.                
  54.                
  55.                 List<Image> input_images = new List<Image>();
  56.                 foreach (PictureBox pic in flowLayoutPanel1.Controls)
  57.                 {
  58.                     input_images.Add((Bitmap)pic.Image);
  59.                 }
  60.                
  61.                 using (FileStream fs = new FileStream(
  62.                     sfdSerialization.FileName, FileMode.Create))
  63.                 {
  64.                     BinaryFormatter formatter = new BinaryFormatter();
  65.                     formatter.Serialize(fs, input_images);
  66.                 }
  67.             }
  68.         }
  69.  
  70.         private void btn_loadimage_Click(object sender, EventArgs e)
  71.         {
  72.             if (ofdSerialization.ShowDialog() == DialogResult.OK)
  73.             {              
  74.                 using (FileStream fs = new FileStream(
  75.                     ofdSerialization.FileName, FileMode.Open))
  76.                 {
  77.                     BinaryFormatter formattter = new BinaryFormatter();
  78.                     List<Image> output_images =
  79.                         (List<Image>)formattter.Deserialize(fs);
  80.  
  81.                     const int margin = 10;
  82.                     int x = margin;
  83.                     int y = 10;
  84.                     foreach (Image image in output_images)
  85.                     {
  86.                         PictureBox pic = new PictureBox();
  87.                         pic.Location = new Point(x, y);
  88.                         pic.SizeMode = PictureBoxSizeMode.AutoSize;
  89.                         pic.Image = image;
  90.                         pic.BorderStyle = BorderStyle.Fixed3D;
  91.                         pic.Parent = flowLayoutPanel1;
  92.                         x += pic.Width;
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.  
  98.         private void btnClear_Click(object sender, EventArgs e)
  99.         {
  100.             flowLayoutPanel1.Controls.Clear();
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement