NEWS

[C#] Hướng dẫn lưu danh sách hình ảnh dạng file nhị phân

[C#] Hướng dẫn lưu danh sách hình ảnh dạng file nhị phân
Đăng bởi: Thảo Meo - Lượt xem: 5704 15:46:47, 30/09/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách lưu trữ nhiều hình ảnh từ thành 1 tệp file nhị phân Binary File trong lập trình C#, Winform.

[C#] Serialize And Deserialize Image to Binary File

Giao diện, demo ứng dụng C#, winform:

image_thumb

Ứng dụng demo này gồm 4 nút chức năng:

Nút 1: Load list hình ảnh vào FlowLayoutPanel

Nút 2: Lưu trữ danh sách file này thành file nhị phân Binary File

Nút 3: Xóa các hình ảnh đang có trên control FlowLayoutPanel

Nút 4: Tải hình ảnh hiển thị từ tệp tin nhị phân mới lưu trữ xong

Video demo ứng dụng:

Full source code C#:

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();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn lưu danh sách hình ảnh dạng file nhị phân
Đăng bởi: Thảo Meo - Lượt xem: 5704 15:46:47, 30/09/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.