- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
[C#] Hướng dẫn tạo hiệu ứng che mờ khuôn mặt (Effect Fixeler Face)
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:
Đầ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!