- [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
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[DEVEXPRESS] Hướng dẫn sử dụng Image Editor trong Picture Edit C#
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 đến các bạn sử dụng Image Editor trong Picture Edit của Devexpress bằng C#.
[DEVEXPRESS] Sử dụng Image Editor trong Picture Edit C#
Từ phiên bản Devexpress 19.1 trở đi, bên Devexpress đã cung cấp cho chúng ta thêm Image Editor dùng để chỉnh sửa hình ảnh trực tiếp trên Picture Edit.
Các bạn có thể download và cài đặt Devexpress 19.1.5 ở đây
Trong công cụ này, giúp chúng ta khi upload hình lên dễ dàng chỉnh sửa một số tính năng cơ bản như:
- Crop Resize image
- Chỉnh sáng tối image
- Convert image sang hình trắng đen
- Rotate image
Ngoài ra, Devexpress cũng giúp chúng ta dễ dàng tích hợp thêm module vào Image Editor này một cách dễ dàng.
Trong bài viết này, mình đã tích hợp thêm chức năng Watermask Image vào menu của Image Editor của Devexpress.
Giao diện demo Image Editor của PictureEdit Devexpress C#:
Để sử dụng Image editor trên PictureEdit của Devexpress các bạn chỉ cần set PictureEdit.ShowEditMenuItem = true;
thì khi right click vào picture edit các bạn sẽ có thêm menu edit để chỉnh sửa hình ảnh với Image Editor.
Dưới đây mình sẽ tích hợp thêm module Watermask vào Edit Image, source code này được cung cấp bởi Devexpress nhé.
Đầu tiên các bạn tạo một UserControl WatermarkToolControl.cs.
Giao diện của WatermarkToolControl:
Source code C# cho WatermarkToolControl.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ImageEditor;
namespace WatermarkDemo {
public partial class WatermarkToolControl : XtraUserControl, IToolSettingsControl {
public event EventHandler Changed;
public WatermarkToolControl()
{
InitializeComponent();
teText.TextChanged += (s, e) => RaiseChanged();
cpeColor.EditValueChanged += (s, e) => RaiseChanged();
seFontSize.EditValueChanged += (s, e) => RaiseChanged();
ceRepeat.CheckedChanged += (s, e) => RaiseChanged();
zoomTrackBarControl1.EditValueChanged += (s, e) => RaiseChanged();
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
teText.Text = "laptrinhvb.net";
cpeColor.Color = Color.FromArgb(180, Color.Silver);
seFontSize.Value = 30;
ceRepeat.Checked = true;
zoomTrackBarControl1.EditValue = 50;
}
void RaiseChanged() {
if(Changed != null)
Changed(this, EventArgs.Empty);
}
public BaseGraphicOperation GetOperation() {
return new WatermarkGraphicOperation(teText.Text, cpeColor.Color, (int)seFontSize.Value, ceRepeat.Checked, (int)zoomTrackBarControl1.Value);
}
}
public class WatermarkGraphicOperation : BaseCachedGraphicOperation {
public string Text { get; protected set; }
public Color Color { get; protected set; }
public bool Repeat { get; protected set; }
public int FontSize { get; protected set; }
public int Zoom { get; protected set; }
public WatermarkGraphicOperation(String text, Color color, int fontSize, bool repeat, int zoom) {
this.Text = text;
this.FontSize = fontSize;
this.Color = color;
this.Repeat = repeat;
this.Zoom = zoom;
}
public override Image Apply(Image input) {
Size newSize = new Size((int)(input.Width * Zoom), (int)(input.Height * Zoom));
var width = (int)(input.Width * Zoom);
var height = (int)(input.Height * Zoom);
Image newImg = new Bitmap(input);
if(!String.IsNullOrEmpty(Text)) {
using (Graphics g = Graphics.FromImage(newImg)) {
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
using (Font font = new Font("Tahoma", FontSize)) {
using(SolidBrush brush = new SolidBrush(this.Color)) {
if(this.Repeat) {
float centerX = ((float)newImg.Width / 2);
float centerY = ((float)newImg.Height / 2);
g.TranslateTransform(centerX, centerY);
g.RotateTransform(-45);
g.TranslateTransform(-centerX, -centerY);
Size textSize = g.MeasureString(Text, font).ToSize();
int max = Math.Max(newImg.Width, newImg.Height);
int start = -Math.Abs(newImg.Width - newImg.Height);
for(int y = start; y < max; y += textSize.Height + 64) {
for(int x = start - (textSize.Width / 2); x < max; x += textSize.Width + 64) {
g.DrawString(Text, font, brush, new PointF(x, y));
}
}
} else {
RectangleF rect = new RectangleF(0, 0, input.Width, input.Height);
using(StringFormat format = new StringFormat()) {
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.DrawString(Text, font, brush, rect, format);
}
}
}
}
//g.DrawImage(newImg, 0, 0, width, height);
}
}
return newImg;
}
}
}
Và tiếp tục là source code trên form main:
using DevExpress.Utils.Svg;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ImageEditor;
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 WatermarkDemo {
public partial class Form1 : XtraForm {
public Form1() {
InitializeComponent();
pictureEdit1.ImageEditorDialogShowing += PictureEdit1_ImageEditorDialogShowing;
}
void PictureEdit1_ImageEditorDialogShowing(object sender, ImageEditorDialogShowingEventArgs e) {
e.Commands.Insert(0, new WatermarkCommand() { Image = svgImageCollection1[0] });
e.Commands.Insert(1, new WatermarkPreset() { Image = svgImageCollection1[1] });
}
void btnShowImageEditor_Click(object sender, EventArgs e) {
pictureEdit1.ShowImageEditorDialog();
}
}
public class WatermarkCommand : IGraphicCommand {
public virtual SvgImage Image {
get;
set;
}
public virtual string ToolTip {
get { return "Add Watermark"; }
}
public virtual void Execute(ImageEditorControl editorControl) {
editorControl.SetActiveTool(new WatermarkToolControl());
}
}
public class WatermarkPreset : WatermarkCommand {
public override string ToolTip {
get { return "Watermark Preset"; }
}
public override void Execute(ImageEditorControl editorControl) {
editorControl.EditController.DoOperation(new WatermarkGraphicOperation("laptrinhvb.net", Color.LightBlue, 15, true, 1));
}
}
}
Video demo Image Editor C# Devexpress:
Thanks for watching!