简体   繁体   中英

drawing a string with a transparent background using C#?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM