- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
[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!