- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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!