- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[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!