ASP.NET:图像上的文字中的粗体字

时间:2009-05-25 01:22:49

标签: asp.net image string

我正在尝试动态地将文字写入图像,但我想在句子中粗体显示一个选定的单词。我所做的是将字符串分成三个字符串,第一部分,粗体字和句子的其余部分。但是,当我尝试将它们绘制到图像(.DrawString())上时,它们不会连接,而是相互覆盖。有没有办法可以在图像上重建一个句子(用中间词加粗)?

谢谢!

编辑:示例代码:

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 个答案:

答案 0 :(得分: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);