- [C#] Sử dụng DotNetBrowser nhân Chromium giải pháp thay thế WebBrowser trên Winform
- [C#] Viết ứng dụng xem và dò kết quả xổ số kiến thiết miền nam
- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
[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!