ASP.NET: Bold Words in Text in Image
I'm trying to dynamically write text to an image, but I would like to highlight the highlighted word in a sentence. What I did is split the line into three lines: the first part, with the bold word, and the rest of the sentence. However, when I try to draw them on the image ( .DrawString()
), they don't merge but overwrite each other. Is there any way to reconstruct the sentence (in bold middle word) in the image?
Thanks!
EDIT: Example code:
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
0
a source to share
1 answer
You need to increase the incremental point when writing out text to a graphic object.
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);
+1
a source to share