I want to add text into icon of the listView , how do I write the code ? You see the image file attached, for this example I add the word "special" color green.

Dani AI

Generated

As observed, permanently compositing text into the image leads to clipping and font/Unicode problems. Building on ’s hint to inspect the ListView class, a cleaner solution is to owner-draw the item at render time: draw the icon, then draw the badge text on top. That avoids generating intermediate files, preserves Unicode (by selecting an appropriate font and renderer), and allows automatic ellipsis, color, DPI-aware placement and per-item logic.

Minimal WinForms example (LargeIcon view):

// Setup (once)
listView1.OwnerDraw = true;
listView1.DrawItem += listView1_DrawItem;

// DrawItem handler
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    Image img = imageList1.Images[e.Item.ImageIndex];
    Rectangle imgRect = new Rectangle(e.Bounds.Left + 4, e.Bounds.Top + 4, imageList1.ImageSize.Width, imageList1.ImageSize.Height);
    e.Graphics.DrawImage(img, imgRect);

    string badge = "Special";
    using (Font font = new Font("Segoe UI", 9f, FontStyle.Bold))
    {
        Rectangle textRect = new Rectangle(imgRect.Left, imgRect.Bottom - 18, imgRect.Width, 18);
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis;
        TextRenderer.DrawText(e.Graphics, badge, font, textRect, Color.Green, flags);
    }

    e.DrawFocusRectangle();
}

Notes and troubleshooting: for Details mode use DrawSubItem/DrawColumnHeader instead of DrawItem; TextRenderer with TextFormatFlags.EndEllipsis prevents lost characters; TextRenderer is fast and good for most Unicode—use Graphics.DrawString with a matching StringFormat when complex script shaping is required. Ensure ImageList.ColorDepth = Depth32Bit and images have alpha if overlaying semi-transparent badges. Avoid modifying ImageList contents at runtime; draw overlays dynamically so other items and DPI scaling are unaffected.

Recommended Answers

All 4 Replies

Perhaps you could have a look here

Your example is very good but still have the disadvantage if the length of the text exceeds the size of the image will be lost some characters and not support Unicode font.

Suggest you study the ListView class type listview c# in Google.
And yes it's a huge beast of a class, but eventually you may find what you want.

There are a few other things that I would like to mention about this topic: Using the ListView :: DrawSubItem event instead of creating an intermediate image file and inserting the text, the material you send me is a good reference, thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.