开发者

drawing a string with a transparent background using C#?

开发者 https://www.devze.com 2023-01-21 13:08 出处:网络
The standard way of g.DrawString creates a gray background. So if overlay another string on the form, part of it appears gray.

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 b开发者_如何学编程e 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消