views:

47

answers:

2

The standard way of g.DrawString creates a gray background. So if overlay another string on the form, part of it appears gray.

My question is, is there any way to draw a string with a transparent background? i want to be able to overlay strings, but still be able to see them.

+1  A: 

Are you sure?

Here's a tutorial, which might help:
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image

(edit)

Try starting from basics: I just created a new forms application and changed the code in Form1 to this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(Form1_Paint);

    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawString("hello", new Font("Arial", 36), new SolidBrush(Color.FromArgb(255,0,0)), new Point(20,20));
        e.Graphics.DrawString("world", new Font("Arial", 36), new SolidBrush(Color.FromArgb(0,0,255)), new Point(30,30));

    }

}

It works as expected, with a transparent background for the text.

Jason Williams
Observehttp://localhostr.com/files/c09365/Capture.JPG
I tried it myself, drawing a string over an image dosent create a grey background. however drawing it on a form does create a gray background
How are you drawing the strings? Posting your code would help... Drawing over text is no different to drawing over a bitmap - to GDI+ they are all just coloured pixels by that stage. However, if you (e.g.) draw text to a bitmap and then blit the bitmap to the Form, or put the text into a control so that it renders it for you, you will get very different results.
Jason Williams
See my answer - I've added some basic font rendering code in a form that demonstrates the transparent background you want.
Jason Williams
+2  A: 

This is impossible to diagnose without you posting code. By default, Graphics.DrawString does not paint the background. This sample form demonstrates this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.DrawString("Underneath", this.Font, Brushes.Black, 0, 0);
        e.Graphics.DrawString("Overlap", this.Font, Brushes.Black, 25, 5);
        base.OnPaint(e);
    }
}

Note how the 'Overlap' string does not erase the 'Underneath' string.

Hans Passant
Well i found out my error, and it was because i wasnt drawing the string inside th epaint event!.
Yes, only draw in OnPaint or the Paint event. Please close your thread by marking it answered.
Hans Passant