简体   繁体   English

使用C#绘制具有透明背景的字符串?

[英]drawing a string with a transparent background using C#?

The standard way of g.DrawString creates a gray background. g.DrawString的标准方法创建灰色背景。 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 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: 尝试从基础开始:我刚刚创建了一个新的表单应用程序,并将Form1中的代码更改为此:

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. 默认情况下,Graphics.DrawString 画背景。 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. 请注意,“重叠”字符串如何不会擦除“下方”字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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