NEWS

[C#] Chèn chú thích dưới hình ảnh lập trình Winform

[C#] Chèn chú thích dưới hình ảnh lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 4351 10:44:11, 21/10/2020C#   In bài viết

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ú.

footer_image_csharp

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#:

insertfooterimage

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!

DONWLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Chèn chú thích dưới hình ảnh lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 4351 10:44:11, 21/10/2020C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.