NEWS

[C#] Hướng dẫn tạo hiệu ứng che mờ khuôn mặt (Effect Fixeler Face)

[C#] Hướng dẫn tạo hiệu ứng che mờ khuôn mặt (Effect Fixeler Face)
Đăng bởi: Thảo Meo - Lượt xem: 4558 13:04:59, 30/05/2020VB.NET   In bài viết

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 tạo hiệu ứng che mờ khuôn mặt (Effect Pixeler) trong lập trình C# Winform.

[C#] Make Image Effect Pixeler Winform

Nếu bạn có để ý thì khi xem tivi hoặc video trên mạng.

Chúng ta sẽ bắt gặp một vài cảnh trong video bị cố tình che hoặc làm mờ đi khuôn mặt (cái này trông quen quen hình như thấy ở đâu rồi.)

Hoặc đối tượng mà Nhà sản xuất hay người làm ra video đó không muốn mọi người nhìn rõ.

Hôm trước mình có chụp hình bằng Zalo thấy có tích hợp sẵn tính năng che mờ, nên hôm nay mình viết bài này cho các bạn nào cần sử dụng:

Dưới đây là hình ảnh demo ứng dụng che mờ khuôn mặt C# winform

EffectPixeler_demo

Đầu tiên, các bạn tạo cho mình một class ImageProcessor.cs với code bên dưới:

using System;
using System.Drawing;

namespace EffectPixeler
{
    public static class ImageProcessor
    {
        public static Bitmap Pixelate(Bitmap image, int blurSize)
        {
            return Pixelate(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
        }

        public static Bitmap Pixelate(Bitmap image, Rectangle rectangle, int pixelateSize)
        {
            Bitmap bitmap = new Bitmap(image.Width, image.Height);
            using (Graphics graphics = Graphics.FromImage((Image) bitmap))
                graphics.DrawImage((Image) image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
            int x1 = rectangle.X;
            while (x1 < rectangle.X + rectangle.Width && x1 < image.Width)
            {
                int y1 = rectangle.Y;
                while (y1 < rectangle.Y + rectangle.Height && y1 < image.Height)
                {
                    int num1 = pixelateSize / 2;
                    int num2 = pixelateSize / 2;
                    while (x1 + num1 >= image.Width)
                        --num1;
                    while (y1 + num2 >= image.Height)
                        --num2;
                    Color pixel = bitmap.GetPixel(x1 + num1, y1 + num2);
                    for (int x2 = x1; x2 < x1 + pixelateSize && x2 < image.Width; ++x2)
                    {
                        for (int y2 = y1; y2 < y1 + pixelateSize && y2 < image.Height; ++y2)
                            bitmap.SetPixel(x2, y2, pixel);
                    }
                    y1 += pixelateSize;
                }
                x1 += pixelateSize;
            }
            return bitmap;
        }

        
    }
}

Và dưới đây là Full source code Effect Fixeler Face C#:

using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Windows;

namespace EffectPixeler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int cropX;
        private int cropY;
        private int cropWidth;
        private int cropHeight;

        public Pen cropPen;
        public int cropPenSize = 1; // 2
        public DashStyle cropDashStyle = DashStyle.Solid;
        public Color cropPenColor = Color.Yellow;
        private void btn_browser_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                picCrop.LoadAsync(dlg.FileName);
            }
        }

        public void SetImageScale(PictureBox pbox, out RectangleF ImgArea, out float zoom)
        {
            SizeF sp = pbox.ClientSize;
            SizeF si = pbox.Image.Size;
            float rp = sp.Width / sp.Height;   // calculate the ratios of
            float ri = si.Width / si.Height;   // pbox and image

            if (rp > ri)
            {
                zoom = 1f * sp.Height / si.Height;
                float width = si.Width * zoom;
                float left = (sp.Width - width) / 2;
                ImgArea = new RectangleF(left, 0, width, sp.Height);
            }
            else
            {
                zoom = 1f * sp.Width / si.Width;
                float height = si.Height * zoom;
                float top = (sp.Height - height) / 2;
                ImgArea = new RectangleF(0, top, sp.Width, height);
            }
        }

        private void pic_origin_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                Cursor = Cursors.Default;
                try
                {
                    float zoom = 1f;
                    RectangleF ImgArea = Rectangle.Empty;

                    SetImageScale(picCrop, out ImgArea, out zoom);
                  

                    var rect = new Rectangle((int) (cropX ), (int)(cropY ), (int)(cropWidth ), (int)(cropHeight));  
                    var cropBitmap = new Bitmap(cropWidth, cropHeight);
                    var bitEffect =  ImageProcessor.Pixelate((Bitmap)picCrop.Image, rect, trackBar1.Value);
                   
                    pic_effect.Image = bitEffect;
                }
                catch (Exception exc)
                {
                }
            }
            catch (Exception exc)
            {
            }
        }

        private void pic_origin_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (picCrop.Image == null)
                    return;

                if (e.Button == MouseButtons.Left)
                {
                    picCrop.Refresh();
                    cropWidth = e.X - cropX;
                    cropHeight = e.Y - cropY;
                    Graphics graphics1 = picCrop.CreateGraphics();
                    graphics1.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
                    graphics1.Dispose();                 
              
                }
            }
            // GC.Collect()

            catch (Exception exc)
            {
                
                    return;
            }
        }

        private void pic_origin_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    cropX = e.X;
                    cropY = e.Y;

                    cropPen = new Pen(cropPenColor, cropPenSize);
                    cropPen.DashStyle = DashStyle.DashDotDot;
                    Cursor = Cursors.Cross;
                }
                picCrop.Refresh();
            }
            catch (Exception exc)
            {
            }
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            lbl_value.Text = trackBar1.Value.ToString();
            try
            {
                Cursor = Cursors.Default;
                try
                {
                    float zoom = 1f;
                    RectangleF ImgArea = Rectangle.Empty;

                    SetImageScale(picCrop, out ImgArea, out zoom);


                    var rect = new Rectangle((int)(cropX), (int)(cropY), (int)(cropWidth), (int)(cropHeight));
                    var cropBitmap = new Bitmap(cropWidth, cropHeight);
                    var bitEffect = ImageProcessor.Pixelate((Bitmap)picCrop.Image, rect, trackBar1.Value);

                    pic_effect.Image = bitEffect;
                }
                catch (Exception exc)
                {
                }
            }
            catch (Exception exc)
            {
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lbl_value.Text = trackBar1.Value.ToString();
        }
    }
}

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn tạo hiệu ứng che mờ khuôn mặt (Effect Fixeler Face)
Đăng bởi: Thảo Meo - Lượt xem: 4558 13:04:59, 30/05/2020VB.NET   In bài viết

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

Đọc tiếp