[C#] Hướng Dẫn Lưu Danh Sách Hình ảnh Dạng File Nhị Phân

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

Tags: serialize image c#deserialize image c#binary file c#

THÔNG TIN TÁC GIẢ

Founder Founder 1285 bài viết 16,793,400
NGUYỄN THẢO

Founder at LaptrinhVB.net

★★★★★

♥ Tình yêu thương chẳng hề hư mất bao giờ. (Cr 13,4)

=========================================================================

My skills includes .NET(C#, VB.NET), DevExpress, Java, Android, PHP,

Python, Sqlserver, Mysql, Reactjs, Dart, Flutter, API services and lot more...

Phone/Zalo/Telegram/WhatsApp: ☎️ (+84)933.913122

Zalo: https://zalo.me/0933913122

Email: [email protected]

My Github: https://github.com/nguyenthao1988

Facebook: https://fb.com/Lewandowski28031988

Youtube Channel: https://www.youtube.com/@thaomeotv      

=========================================================================

" Gửi tặng tác giả: Thảo Meo ly cà phê đầy năng lượng và cảm hứng. "

Mời Ad ly cafe

BÀI VIẾT LIÊN QUAN

Từ khóa » Code Lưu ảnh Trong C#