2009-05-25 17 views
0

我試圖動態地向文本中寫入文本,但是我想在文本中粗體選擇一個單詞。我所做的是將字符串分成三個字符串,第一部分,粗體字和剩餘部分。但是,當我嘗試將它們繪製到圖像上時(.DrawString()),它們不會連接,而是相互覆蓋。有什麼方法可以在圖像上重建一個句子(粗體顯示一箇中間單詞)?ASP.NET:在圖像上的文本中的粗體字

謝謝!

編輯:示例代碼:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim w As Word = Word.GetLastPublishedWord() 
    Dim wordForm As String = Word.FindWordForm(w.Word, w.Sentence, Word.RegexOutputType.StandardString) 
    Dim firstPart As String = Left(w.Sentence, w.Sentence.IndexOf(wordForm)) 
    Dim lastPart As String = Right(w.Sentence, (w.Sentence.Length - firstPart.Length - wordForm.Length)) 

    Dim sig As Image = Image.FromFile(Server.MapPath(ResolveUrl("~/images/sig.jpg"))) 
    Dim text As Graphics = Graphics.FromImage(sig) 
    text.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 
    Dim sentenceRec As New RectangleF(0, 0, 400, 75) 
    Dim tagRec As New RectangleF(250, 75, 150, 25) 
    text.DrawString(firstPart, New Font("Arial", 12, FontStyle.Regular), SystemBrushes.WindowText, sentenceRec) 
    text.DrawString(wordForm, New Font("Arial", 12, FontStyle.Bold), SystemBrushes.WindowText, sentenceRec) 
    text.DrawString(lastPart, New Font("Arial", 12, FontStyle.Regular), SystemBrushes.WindowText, sentenceRec) 

    Response.ContentType = "image/jpeg" 
    sig.Save(Response.OutputStream, ImageFormat.Jpeg) 
    sig.Dispose() 
    text.Dispose() 
End Sub 

回答

1

你需要爲你寫出來的文字到圖形對象遞增插入點。

PointF insertionPoint; 
SizeF textWidth = g.MeasureString("First ", normalFont); 

g.DrawString("First ", normalFont, Brushes.Black, insertionPoint); 

insertionPoint.X += textWidth.Width; 
textWidth = g.MeasureString("bolded", boldFont); 
g.DrawString("bolded", boldFont, Brushes.Black, insertionPoint); 

insertionPoint.X += textWidth.Width; 
g.DrawString(" and remaining.", normalFont, Brushes.Black, insertionPoint); 
+0

與此唯一的問題是,文本將包裝...這就是爲什麼我包括RectangleF,這是一個佈局矩形。任何其他建議? – Jason 2009-05-25 04:10:18