- [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#] Sử dụng control PictureBox Color Overlay Winform
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách tạo overlay color trên picturebox C#, Winform.
[C#] Overlay Color in PictureBox Winform.
Dưới đây, là giao diện mình Overlay Color trên PictureBox:
Khi các bạn thiết kế ứng dụng theo dạng giao diện hiện đại Modern thì overlay picture sẽ giúp bạn làm ứng dụng trông đẹp và bắt mắt hơn.
Control này hỗ trợ các bạn: Opacity và Border Radius trên pictureBox
Video Demo Overlay PictureBox C#, Winform:
Đầu tiên, các bạn tạo cho mình một class VBImageColorOverlay.cs
với nội dung bên dưới:
using ImageColorOverlay.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ImageColorOverlay
{
public class VBImageColorOverlay : Panel
{
private int _opacityOverlay;
private int af;
private Color _backgroundColorOverlay;
private int _borderRadius;
private bool _overlayColorDefault;
public VBImageColorOverlay()
{
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
this.BackgroundImage = (Image)Resources.background;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.TabStop = false;
this.Size = new Size(250, 250);
this.OpacityOverlay = 50;
this._backgroundColorOverlay = Color.MediumSlateBlue;
}
[Category("Custom Props")]
public bool OverlayColorDefault
{
get => this._overlayColorDefault;
set => this._overlayColorDefault = value;
}
[Category("Custom Props")]
public int OpacityOverlay
{
get => this._opacityOverlay;
set
{
if (value < 0 || value > 100)
return;
this._opacityOverlay = value;
this.af = Convert.ToInt32((double)this._opacityOverlay / 100.0 * (double)byte.MaxValue);
if (!this.DesignMode)
return;
this.Invalidate(false);
}
}
[Category("Custom Props")]
public Color BackgroundColorOverlay
{
get => this._backgroundColorOverlay;
set
{
this._backgroundColorOverlay = value;
if (!this.DesignMode)
return;
this.Invalidate(false);
}
}
[Category("Custom Props")]
public Image VBBackgroundImage
{
get => this.BackgroundImage;
set => this.BackgroundImage = value;
}
[Category("Custom Props")]
public ImageLayout ImageLayout
{
get => this.BackgroundImageLayout;
set => this.BackgroundImageLayout = value;
}
[Category("Custom Props")]
public int BorderRadius
{
get => this._borderRadius;
set
{
this._borderRadius = value;
this.Invalidate();
}
}
private void hgsgh()
{
if (this._overlayColorDefault)
return;
this._backgroundColorOverlay = Color.FromArgb(83, 97, 212);
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
this.hgsgh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
using (SolidBrush solidBrush = new SolidBrush(Color.FromArgb(this.af, this._backgroundColorOverlay)))
{
graphics.FillRectangle((Brush)solidBrush, this.ClientRectangle);
ppwag.dsadsa((Control)this, this._borderRadius, graphics);
}
}
}
public static class ppwag
{
private static GraphicsPath gfhfg(Rectangle daas, float hghg)
{
GraphicsPath graphicsPath = new GraphicsPath();
float num = hghg * 2f;
graphicsPath.StartFigure();
graphicsPath.AddArc((float)daas.X, (float)daas.Y, num, num, 180f, 90f);
graphicsPath.AddArc((float)daas.Right - num, (float)daas.Y, num, num, 270f, 90f);
graphicsPath.AddArc((float)daas.Right - num, (float)daas.Bottom - num, num, num, 0.0f, 90f);
graphicsPath.AddArc((float)daas.X, (float)daas.Bottom - num, num, num, 90f, 90f);
graphicsPath.CloseFigure();
return graphicsPath;
}
public static void dasfff(Control gdfgd, int hhfghfg)
{
if (hhfghfg >= 1)
{
using (GraphicsPath path = ppwag.gfhfg(gdfgd.ClientRectangle, (float)hhfghfg))
gdfgd.Region = new Region(path);
gdfgd.Resize += (EventHandler)((s, e) =>
{
using (GraphicsPath path = ppwag.gfhfg(gdfgd.ClientRectangle, (float)hhfghfg))
gdfgd.Region = new Region(path);
});
}
else
{
gdfgd.Region = new Region(gdfgd.ClientRectangle);
gdfgd.Resize += (EventHandler)((s, e) => gdfgd.Region = new Region(gdfgd.ClientRectangle));
}
}
public static void dsadsa(Control hhgfh, int gjh, Graphics gr)
{
if (gjh > 1)
{
using (GraphicsPath path = ppwag.gfhfg(hhgfh.ClientRectangle, (float)gjh))
{
using (Pen pen = new Pen(hhgfh.Parent.BackColor, 1f))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
hhgfh.Region = new Region(path);
gr.DrawPath(pen, path);
}
}
}
else
hhgfh.Region = new Region(hhgfh.ClientRectangle);
}
public static void dadasf(
Control gdgdfg,
int hgfhfgh,
Graphics hgjgh,
Color hfghfg,
float wqdf)
{
if (hgfhfgh > 1)
{
using (GraphicsPath path = ppwag.gfhfg(gdgdfg.ClientRectangle, (float)hgfhfgh))
{
using (Pen pen1 = new Pen(gdgdfg.Parent.BackColor, wqdf + 1f))
{
using (Pen pen2 = new Pen(hfghfg, wqdf))
{
using (Matrix matrix = new Matrix())
{
hgjgh.SmoothingMode = SmoothingMode.AntiAlias;
gdgdfg.Region = new Region(path);
hgjgh.DrawPath(pen1, path);
if ((double)wqdf < 1.0)
return;
Rectangle clientRectangle = gdgdfg.ClientRectangle;
float scaleX = (float)(1.0 - ((double)wqdf + 1.0) / (double)clientRectangle.Width);
float scaleY = (float)(1.0 - ((double)wqdf + 1.0) / (double)clientRectangle.Height);
matrix.Scale(scaleX, scaleY);
matrix.Translate(wqdf / 1.6f, wqdf / 1.6f);
hgjgh.Transform = matrix;
hgjgh.DrawPath(pen2, path);
}
}
}
}
}
else
{
gdgdfg.Region = new Region(gdgdfg.ClientRectangle);
if ((double)wqdf >= 1.0)
{
using (Pen pen = new Pen(hfghfg, wqdf))
{
pen.Alignment = PenAlignment.Inset;
hgjgh.DrawRectangle(pen, 0.0f, 0.0f, (float)gdgdfg.Width - 0.5f, (float)gdgdfg.Height - 0.5f);
}
}
}
}
}
}
Sau khi tạo xong, các bạn build project để hiển thị control VBImageColorOverlay bên trái ở thanh Toolbox, và các bạn kéo ra để sử dụng:
Source code FrmMain.cs
:
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;
namespace ImageColorOverlay
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void colorWheel_ColorChanged(object sender, EventArgs e)
{
vbImageColorOverlay1.BackgroundColorOverlay = colorWheel.Color;
vbImageColorOverlay1.Refresh();
}
private void bar_opacity_ValueChanged(object sender, EventArgs e)
{
vbImageColorOverlay1.OpacityOverlay = bar_opacity.Value;
vbImageColorOverlay1.Refresh();
}
private void bar_borderRadius_ValueChanged(object sender, EventArgs e)
{
vbImageColorOverlay1.BorderRadius = bar_borderRadius.Value;
vbImageColorOverlay1.Refresh();
}
}
}
Thanks for watching!