- [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#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Chèn chú thích dưới hình ảnh lập trình 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 thêm chú thích vào dưới hình ảnh Image trong lập trình C#, Winform.
[C#] Insert Description Below Image
Khi các bạn đọc các bài báo, thì cuối hình ảnh thường có dòng chú thích để ghi rõ cho người đọc biết hình ảnh đó là hình ảnh gì, của người nào, làm công việc gì,...
Ví dụ: Mình có hình ảnh Ca Sĩ Thủy Tiên ở bên dưới, đang thực hiện cứu trợ cho đồng bào lũ lụt miền trung.
Các bạn có thể dễ dàng thay đổi: Màu nền , màu chữ, font chữ, chiều cao và lề của chữ ghi chú.
Và khi có tấm hình, chúng ta sẽ ghi thêm dòng chú thích vào bên dưới.
Demo ứng dụng chèn chú thích vào hình ảnh C#:
Full source code tạo chú thích hình ảnh C#:
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 AddFooterImage
{
public partial class Form1 : Form
{
Label lblSample;
public Form1()
{
InitializeComponent();
lblSample = new Label();
}
private void btn_background_Click(object sender, EventArgs e)
{
colorDialog1.Color = lblSample.BackColor;
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
lblSample.BackColor = colorDialog1.Color;
}
}
private void btn_font_Click(object sender, EventArgs e)
{
fontDialog1.Font = lblSample.Font;
fontDialog1.Color = lblSample.ForeColor;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
lblSample.Font = fontDialog1.Font;
lblSample.ForeColor = fontDialog1.Color;
}
}
private StringFormat GetStringFormat()
{
StringFormat sf = new StringFormat();
if (radUL.Checked || radUM.Checked || radUR.Checked)
sf.LineAlignment = StringAlignment.Near;
else if (radML.Checked || radMM.Checked || radMR.Checked)
sf.LineAlignment = StringAlignment.Center;
else if (radLL.Checked || radLM.Checked || radLR.Checked)
sf.LineAlignment = StringAlignment.Far;
if (radUL.Checked || radML.Checked || radLL.Checked)
sf.Alignment = StringAlignment.Near;
else if (radUM.Checked || radMM.Checked || radLM.Checked)
sf.Alignment = StringAlignment.Center;
else if (radUR.Checked || radMR.Checked || radLR.Checked)
sf.Alignment = StringAlignment.Far;
return sf;
}
private void button1_Click(object sender, EventArgs e)
{
using (Bitmap bm = Properties.Resources.thuytien3)
{
pictureBox1.Image = AddDescriptionToBitmap(bm);
}
}
private Bitmap AddDescriptionToBitmap(Bitmap bm)
{
int label_height = 0;
try
{
label_height = int.Parse(txtMargin.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
Bitmap new_bm = new Bitmap(bm.Width, bm.Height + label_height);
Rectangle rect = new Rectangle(
0, bm.Height, bm.Width, label_height);
using (StringFormat sf = GetStringFormat())
{
using (Graphics gr = Graphics.FromImage(new_bm))
{
gr.DrawImage(bm, 0, 0);
using (SolidBrush brush = new SolidBrush(lblSample.BackColor))
{
gr.FillRectangle(brush, rect);
brush.Color = lblSample.ForeColor;
gr.DrawString(txtDescription.Text, lblSample.Font,
brush, rect, sf);
}
}
}
return new_bm;
}
}
}
Thanks for watching!