DotImage使用教程: 动态调整Text Annotation的字体大小

原创|使用教程|编辑:龚雪|2014-05-14 09:39:19.000|阅读 487 次

概述:本文主要介绍在使用DotImage的过程中如何动态调整Text Annotation的字体大小。

# 您正在找协同办公软件吗?点击这里站长给您推荐 #

相关链接:

在WinForms项目中,当调整TextAnnotation时,.Net图像处理控件DotImage会自动换行TextAnnotation中的文本。如果你想动态改变字体大小本身,你可以在 AnnotateViewer的AnnotationResized事件中进行如下操作:

C# Sample:

-----

private void annotateViewer1_AnnotationResized(object sender, AnnotationEventArgs e)

{

ResizeFont(e.Annotation);

}

private void ResizeFont(AnnotationUI baseAnno)

{

TextAnnotation tAnno = baseAnno as TextAnnotation;

if (tAnno != null)

{

Size annoSize = tAnno.Size.ToSize();

System.Drawing.Font annoFont = new Font(tAnno.Font.Name, tAnno.Font.Size);

SizeF size;

System.Drawing.Font newFont = AppropriateFont(annotateViewer1.CreateGraphics(),

12f,

72f,

annoSize,

tAnno.Text,

annoFont,

out size);

tAnno.Font = new AnnotationFont(newFont.Name, newFont.Size);

}

}

public static Font AppropriateFont(Graphics g, float minFontSize,

float maxFontSize, Size layoutSize, string s, Font f, out SizeF extent)

{

if (maxFontSize == minFontSize)

f = new Font(f.FontFamily, minFontSize, f.Style);

extent = g.MeasureString(s, f);

if (maxFontSize <= minFontSize)

return f;

float hRatio = layoutSize.Height / extent.Height;

float wRatio = layoutSize.Width / extent.Width;

float ratio = (hRatio < wRatio) ? hRatio : wRatio;

float newSize = f.Size * ratio;

if (newSize < minFontSize)

newSize = minFontSize;

else if (newSize > maxFontSize)

newSize = maxFontSize;

f = new Font(f.FontFamily, newSize, f.Style);

extent = g.MeasureString(s, f);

return f;

}

VB.Net Sample:

-----

Private Sub annotateViewer1_AnnotationResized(ByVal sender As Object, ByVal e AsAnnotationEventArgs)

ResizeFont(e.Annotation)

End Sub

Private Sub ResizeFont(ByVal baseAnno As AnnotationUI)

Dim tAnno As TextAnnotation = TryCast(baseAnno, TextAnnotation)

If tAnno IsNot Nothing Then

Dim annoSize As Size = tAnno.Size.ToSize()

Dim annoFont As System.Drawing.Font = New Font(tAnno.Font.Name, tAnno.Font.Size)

Dim size As SizeF

Dim newFont As System.Drawing.Font = AppropriateFont(annotateViewer1.CreateGraphics(), 12.0F, 72.0F, annoSize, tAnno.Text, annoFont, _

size)

tAnno.Font = New AnnotationFont(newFont.Name, newFont.Size)

End If

End Sub

Public Shared Function AppropriateFont(ByVal g As Graphics, ByVal minFontSize As Single, ByValmaxFontSize As Single, ByVal layoutSize As Size, ByVal s As String, ByVal f As Font, _

ByRef extent As SizeF) As Font

If maxFontSize = minFontSize Then

f = New Font(f.FontFamily, minFontSize, f.Style)

End If

extent = g.MeasureString(s, f)

If maxFontSize <= minFontSize Then

Return f

End If

Dim hRatio As Single = layoutSize.Height / extent.Height

Dim wRatio As Single = layoutSize.Width / extent.Width

Dim ratio As Single = If((hRatio < wRatio), hRatio, wRatio)

Dim newSize As Single = f.Size * ratio

If newSize < minFontSize Then

newSize = minFontSize

ElseIf newSize > maxFontSize Then

newSize = maxFontSize

End If

f = New Font(f.FontFamily, newSize, f.Style)

extent = g.MeasureString(s, f)

Return f

End Function

 



标签:.NET图像处理

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果

登录 慧都网发表评论


暂无评论...

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关厂商
相关产品
DotImage

一款可以实现图像和PDF的浏览、捕捉和注释等功能的.NET图形处理SDK。

在线
客服
咨询
电话
400-700-1020
在线
QQ
购物车 反馈 返回
顶部
在线客服系统
live chat