NEWS

[C#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform

[C#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform
Đăng bởi: Thảo Meo - Lượt xem: 1736 13:35:01, 15/06/2023DEVEXPRESS   In bài viết

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!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform
Đăng bởi: Thảo Meo - Lượt xem: 1736 13:35:01, 15/06/2023DEVEXPRESS   In bài viết

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

Đọc tiếp
.