Search code examples
c#gdi

Trying to add transparent text with TextRenderer.DrawText to a bitmap image


I'm actually trying to add some text to an image in C# with

System.Windows.Forms.TextRenderer.DrawText(Graphics, string, Rectangle, Color, TextFormaFlags)

I prepare my image (which is a png) by loading it in memory, with something similar to

Image image = ImageCache.Get(...);
bitmap = new Bitmap(image);
graphic = Graphics.FromImage(bitmap);

I then draw my text with the above command. The problem is that whatever I use for the color, even something like

System.Drawing.Color.FromArgb(0,255,255,255)

the transparency is not drawn. I tried many settings for

graphics.TextRenderingHint

and different combinations of fonts, transparency level, etc. Is there something I don't understand here? Any hint is appreciated.

Thank you.


Solution

  • As mentionned in the comments : if you try to draw transparent text with

    System.Windows.Forms.TextRenderer.DrawText
    

    because you look for the advantages brought by GDI in C#, you just can't. Use

    System.Drawing.Graphics.DrawString
    

    instead, even if the result for the word-wrapping is slightly inferior with GDI+.