2010-10-24 87 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