2010-10-24 90 views
1

g.DrawString的标准方式创建一个灰色背景。所以如果在窗体上覆盖另一个字符串,它的一部分会显示为灰色。用C#绘制一个透明背景的字符串?

我的问题是,有什么办法绘制一个透明背景的字符串?我想能够覆盖字符串,但仍然能够看到它们。

回答

4

你确定吗?

这里有一个教程,这可能有助于:
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image

(编辑)

尝试从基础做起:我刚刚创建了一个新的形式的应用,改变了代码在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)); 

    } 

} 

它按预期工作,具有透明背景的文字。

+0

观察 http://localhostr.com/files/c09365/Capture.JPG – user478636 2010-10-24 16:20:03

+0

我自己尝试过,在图像dosent上绘制一个字符串,创建一个灰色背景。 但是在表单上绘制它确实会创建一个灰色背景 – user478636 2010-10-24 16:21:00

+0

如何绘制字符串?发布代码将有助于......绘制文本与绘制位图没有什么不同 - 对于GDI +,它们在该阶段都只是彩色像素。但是,如果您(例如)将文本绘制到位图,然后将位图移动到窗体上,或者将文本放入控件中以便为您呈现它,则会得到非常不同的结果。 – 2010-10-24 16:56:00

2

这是不可能的诊断没有你张贴代码。默认情况下,Graphics.DrawString确实是而不是绘制背景。此示例的形式说明了这一点:

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); 
    } 
} 

注意如何“重叠”的字符串不删除“底下”的字符串。

+0

嗯,我发现我的错误,这是因为我没有绘制字符串内的epaint事件! – user478636 2010-10-24 17:52:41

+1

是的,只能在OnPaint或Paint事件中绘制。请通过标记回答来关闭你的线程。 – 2010-10-24 17:58:18