NEWS

[C#] Hướng dẫn thêm text vào hình ảnh icon winform

[C#] Hướng dẫn thêm text vào hình ảnh icon winform
Đăng bởi: Thảo Meo - Lượt xem: 3995 14:43:23, 08/04/2021EBOOK

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 vẽ chữ số lên Icon có sẵn trong lập trình C#, Winform.

Mình sẽ viết một ứng dụng nhỏ đemo các bạn có thể áp dụng khi download file, hay làm gì về progress thì có thể thay đổi giá trị bằng Icon để hiển thị như demo mình dưới đây.

icon_progress

Hoặc nếu các bạn có viết ứng dụng giống như đo nhiệt độ hay phần trăm làm việc CPU hay Ram bạn cũng có thể cho nó hiển thị ở phần notifyIcon C#.

Để nhìn trực quan một các dễ dàng.

Full source code Draw Text to Icon C#, Winform:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreateIconFromText
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(() => {
                for (int i = 0; i <= 99; i++)
                {
                    var icon = GetIcon(i.ToString());
                    BeginInvoke(new Action(() => {

                        this.Icon = icon;
                        notifyIcon1.Icon = icon;
                    }));

                    Thread.Sleep(50);
                }
               
            });
           
           
        }

        public static Icon GetIcon(string text)
        {         
            Bitmap bitmap = new Bitmap(32, 32);
            var sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            Icon icon = new Icon(@"Custom-Icon-Design-Pretty-Office-9-Square.ico");
               Font drawFont = new System.Drawing.Font("Tahoma", 11, FontStyle.Bold);
               SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);
               Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);

            graphics.DrawIcon(icon, 0, 0);
            graphics.DrawString(text, drawFont, drawBrush, new Rectangle(0, 0, bitmap.Width, bitmap.Height), sf);          
            bitmap.Save("icon.ico", System.Drawing.Imaging.ImageFormat.Icon);
            Icon createdIcon = Icon.FromHandle(bitmap.GetHicon());
            drawFont.Dispose();
            drawBrush.Dispose();
            graphics.Dispose();
            bitmap.Dispose();

            return createdIcon;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            var icon = GetIcon("VB");
          
                this.Icon = icon;
              
            notifyIcon1.Icon = this.Icon;
            notifyIcon1.Visible = true;
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: draw text to icon c#draw icon c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn thêm text vào hình ảnh icon winform
Đăng bởi: Thảo Meo - Lượt xem: 3995 14:43:23, 08/04/2021EBOOK