- [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
[C#] Hướng dẫn chèn text vào dưới hình ảnh winform
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 chèn thêm Text vào dưới hình ảnh trong lập trình C# winform.
[C#] Add Text into Image Winform
Dưới đây là giao diện demo ứng dụng:
Ở hình trên, các bạn thấy mình có một con dấu, và mình muốn là chèn thêm Tên, ngày tháng giống như chữ ký vào dưới và canh giữa một hình ảnh.
Cách thực hiện:
Bước 1: Mình sẽ vẽ thêm một bitmap hình ảnh từ text của mình nhập vào canh đúng bằng width của hình gốc.
Bước 2: mình sẽ merge hai tấm hình: con dấu và text thành một image.
Full source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddTextToImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
if(dlg.ShowDialog() == DialogResult.OK)
{
var a = DrawLetter(DateTime.Now.ToString("dd/MM/yyyy") + "\n" + textBox1.Text, dlg.FileName);
pictureBox2.Image = Image.FromFile(dlg.FileName);
pictureBox1.Image = a;
}
}
private Image DrawLetter(string letter, string urlImage)
{
var img = (Bitmap)Image.FromFile(urlImage);
var bit = new Bitmap(img.Width, 100);
Graphics g = Graphics.FromImage(bit);
float width = ((float)bit.Width);
float height = ((float)bit.Height);
float emSize = height;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.InterpolationMode = InterpolationMode.High;
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Far;
// Top/Left.
sf.Alignment = StringAlignment.Center;
Font font = new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular);
Rectangle rect = new Rectangle(5, 5, bit.Width - 10, bit.Height - 10);
font = FindBestFitFont(g, letter.ToString(), font, bit.Size);
SizeF size = g.MeasureString(letter.ToString(), font);
g.DrawString(letter, font, new SolidBrush(Color.Red), rect, sf);
return MergeTwoImages(img, bit);
}
public Bitmap MergeTwoImages(Image firstImage, Image secondImage)
{
if (firstImage == null)
{
throw new ArgumentNullException("firstImage");
}
if (secondImage == null)
{
throw new ArgumentNullException("secondImage");
}
int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;
int outputImageHeight = firstImage.Height + secondImage.Height + 1;
Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),
new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);
graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);
}
return outputImage;
}
private Font FindBestFitFont(Graphics g, String text, Font font, Size proposedSize)
{
while (true)
{
SizeF size = g.MeasureString(text, font);
if (size.Height <= proposedSize.Height &&
size.Width <= proposedSize.Width) { return font; }
Font oldFont = font;
font = new Font(font.Name, (float)(font.Size * .9), font.Style);
oldFont.Dispose();
}
}
}
}
Thanks for watching!