- [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#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform
Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn source code cách tạo hiệu ứng Pixel Image trên Csharp winform.
[C#] How to make Pixel Image Effect in Winform
Dưới đây, là video demo ứng dụng:
Source code C#:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Timer = System.Windows.Forms.Timer;
namespace ImagePixelEffect
{
public partial class Form1 : Form
{
public List<Bitmap> _bitmaps = new List<Bitmap>();
public Random _random = new Random();
public Form1()
{
InitializeComponent();
}
public void RunProcessing(Bitmap bitmap) {
var pixels = GetPixels(bitmap);
var pixelsInStep = (bitmap.Width * bitmap.Height) / 100;
var currentPixelsSet = new List<Pixel>(pixels.Count - pixelsInStep);
for (int i = 0; i < trackBar1.Maximum; i++) {
for (int j = 0; j < pixelsInStep; j++) {
var index = _random.Next(pixels.Count);
currentPixelsSet.Add(pixels[index]);
pixels.RemoveAt(index);
}
var currentBitmap= new Bitmap(bitmap.Width, bitmap.Height);
foreach (var pixel in currentPixelsSet) {
currentBitmap.SetPixel(pixel.Point.X, pixel.Point.Y,pixel.Color);
}
_bitmaps.Add(currentBitmap);
this.BeginInvoke(new Action(() => { this.Text = $"{i}%"; }));
}
_bitmaps.Add(bitmap);
}
public List<Pixel> GetPixels(Bitmap bitmap)
{
var pixels = new List<Pixel>(bitmap.Width * bitmap.Height);
for (int y = 0; y< bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
pixels.Add(new Pixel() {
Color = bitmap.GetPixel(x, y),
Point = new Point() { X = x, Y = y }
});
}
}
return pixels;
}
private async void btnOpen_Click(object sender, EventArgs e)
{
var opendialog = new OpenFileDialog();
if (opendialog.ShowDialog() == DialogResult.OK) {
pictureBox1.Image = null;
_bitmaps.Clear();
var bitmap = new Bitmap(opendialog.FileName);
await Task.Run(() =>
{
RunProcessing(bitmap);
});
}
}
public void SaveBitmaps(List<Bitmap> bitmaps, string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, bitmaps);
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
if(_bitmaps == null || _bitmaps.Count == 0) return;
pictureBox1.Image = _bitmaps[trackBar1.Value -1];
}
private void button1_Click(object sender, EventArgs e)
{
SaveBitmaps(_bitmaps, "data.bin");
}
public List<Bitmap> LoadBitmaps(string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
List<Bitmap> bitmaps = (List<Bitmap>)formatter.Deserialize(fileStream);
return bitmaps;
}
}
private void button2_Click(object sender, EventArgs e)
{
_bitmaps.Clear();
_bitmaps = LoadBitmaps("data.bin");
}
private void Form1_Load(object sender, EventArgs e)
{
//try
//{
// _bitmaps.Clear();
// _bitmaps = LoadBitmaps("data.bin");
//}
//catch (Exception)
//{
//}
}
private Timer timer;
private int scrollInterval = 10; // Interval in milliseconds
private int scrollStep = 1;
private int value = 1;
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
//timer = new Timer();
//timer.Interval = scrollInterval;
//timer.Tick += Timer_Tick;
//timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
value += scrollStep;
if (_bitmaps == null || _bitmaps.Count == 0) return;
pictureBox1.Image = _bitmaps[value - 1];
if (value >= 100)
{
timer.Stop();
timer.Dispose();
}
}
}
}
Thanks for watching!