2009-05-25 4 views
0

イメージにテキストを動的に書き込もうとしていますが、文章の中で選択した単語を太字にしたいと思います。私がしたことは、文字列を3つの文字列、最初の部分、太字にする単語、および文の残りの部分に分けることです。ただし、イメージ(.DrawString())に描画しようとすると、連結されずに互いに上書きされます。画像上に文章(中間語を太字にする)を再構成する方法はありますか?ASP.NET:イメージ上のテキストの太字の単語

ありがとうございます!

EDIT:コード例:

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

+0

つまり、テキストを400pxでラップできるようにしたい – Jason

+0

残念ながら、GDI +には「リッチテキスト」を自動的に処理するものは何もありません。ラッピングを処理するために独自のロジックをロールバックする必要があります。参考までに、GDI +を完全に使用するhtml CSS2レンダラーを作成するhttp://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspxを見てください。 –