- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[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:
Ứ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!