NEWS

[C#] Hướng dẫn chèn text vào dưới hình ảnh winform

[C#] Hướng dẫn chèn text vào dưới hình ảnh winform
Đăng bởi: Thảo Meo - Lượt xem: 7893 10:52:46, 22/05/2020DEVEXPRESS   In bài viết

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:

add_text_to_image

Ở 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!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn chèn text vào dưới hình ảnh winform
Đăng bởi: Thảo Meo - Lượt xem: 7893 10:52:46, 22/05/2020DEVEXPRESS   In bài viết

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

Đọc tiếp
.