定心功能缺陷

时间:2016-10-06 19:36:06

标签: .net vb.net math graphics

我有一个以文字为中心的功能,但我在那里的某个地方有一个数学上的缺陷,我只是没有发现。

enter image description here

正如你所看到的那样,字体偏向底部和右边,部分阴影被切断了,尽管我认为它已经占了它。

矩形越大,瑕疵就越明显。

也许这甚至是我使用Graphics.DrawImage函数的一个缺陷,因为对我来说这个函数看起来很好。

有人看到我的错误吗?

感谢您的帮助!

Public Sub pDraw(ByVal uGraphics As Graphics, ByVal uText As String, ByRef uRect As Rectangle, ByVal uFont As Font, ByVal uStringFormat As StringFormat)

    'I have established nice values using a font size of 50. So if we project them onto other font sizes, we will use these templates and percentages
    Dim cstOutlineThickness As Integer = 4
    Dim cstShadowX As Integer = 1
    Dim cstShadowY As Integer = 4
    Dim cstDefaultFontSize As Integer = 50

    Dim nPngOutlineText As New PngOutlineText()
    nPngOutlineText.TextGradOutline(
                    Color.FromArgb(56, 106, 150),
                    Color.FromArgb(255, 0, 56, 113),
                    Color.FromArgb(255, 0, 56, 113),
                    0)
    nPngOutlineText.EnableReflection(False)
    nPngOutlineText.EnableShadow(False)

    'we can add the border and shadow sizes later. These are fixed values!
    Dim fDestWidth As Single
    Dim fDestHeight As Single
    SetGoodFontSize(nPngOutlineText, uGraphics, uText, uFont, uRect, fDestWidth, fDestHeight, 4000)

    Dim sngPerc As Single = modConversions.Percent(cstDefaultFontSize, uFont.Size, False)

    Dim iOutlineThickness As Integer = (sngPerc * cstOutlineThickness) / 100
    Dim iShadowX As Integer = (sngPerc * cstShadowX) / 100
    Dim iShadowY As Integer = (sngPerc * cstShadowY) / 100

    uGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    uGraphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

    Dim m_PngOutlineText As New PngOutlineText()
    m_PngOutlineText.TextGradOutline(
                    Color.FromArgb(56, 106, 150),
                    Color.FromArgb(255, 0, 56, 113),
                    Color.FromArgb(255, 0, 56, 113),
                    iOutlineThickness)
    m_PngOutlineText.EnableReflection(False)
    m_PngOutlineText.EnableShadow(True)

    m_PngOutlineText.DiffusedShadow(
    Color.FromArgb(10, 0, 0, 0),
   8,
    New Point(iShadowX, iShadowY))

    '10,0,0,0
    Dim fDestWidth2 As Single = 0
    Dim fDestHeight2 As Single = 0
    Dim sngStartX2 As Single = 0
    Dim sngStartY2 As Single = 0
    m_PngOutlineText.MeasureString(uGraphics, uFont.FontFamily, uFont.Style, CInt(uFont.Size), uText, New Point(0, 0), StringFormat.GenericDefault, sngStartX2, sngStartY2, fDestWidth2, fDestHeight2)

    'center it. There is no good way to specify the aligning in the font-drawing
    Dim r As Rectangle = Rectangle.FromLTRB(0, 0, fDestWidth2, fDestHeight2)
    CenterRect(uRect, r)

    Dim m_clrBkgd As Color = Color.FromArgb(255, 255, 255)
    m_PngOutlineText.SetShadowBkgd(m_clrBkgd, fDestWidth2, fDestHeight2)

    Dim m_pPngImage As Bitmap = New Bitmap(CInt(fDestWidth2), CInt(fDestHeight2), System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    m_PngOutlineText.SetPngImage(m_pPngImage)
    Dim gradientBrush As New LinearGradientBrush(New RectangleF(0, 0, fDestWidth2, fDestHeight2), Color.FromArgb(56, 106, 150), Color.FromArgb(37, 119, 178), Drawing2D.LinearGradientMode.Vertical)
    m_PngOutlineText.TextGradOutline(gradientBrush, Color.FromArgb(255, 0, 56, 113), Color.FromArgb(255, 0, 56, 113), iOutlineThickness)
    m_PngOutlineText.DrawString(uGraphics, New FontFamily(uFont.Name), uFont.Style, uFont.Size, uText, New Point(0, 0), uStringFormat)

    uGraphics.DrawImage(m_pPngImage, r)

End Sub

Public Sub CenterRect(ByVal uMain As Rectangle, ByRef uRectToCenter As Rectangle)

    Dim iMainLeft As Integer = uMain.Left
    Dim iMainTop As Integer = uMain.Top
    Dim iMainWidth As Integer = uMain.Width
    Dim iMainHeight As Integer = uMain.Height
    Dim iDestW As Integer = uRectToCenter.Width
    Dim iDestH As Integer = uRectToCenter.Height
    Dim iDestX2 As Integer = iMainLeft + ((iMainWidth - iDestW) / 2)
    Dim iDestY2 As Integer = iMainTop + ((iMainHeight - iDestH) / 2)

    Dim nPoint As New Point(iDestX2, iDestY2)
    uRectToCenter.Location = nPoint

End Sub

以下是调试的另一个屏幕截图: 如您所见,图像是在X = 22,Y = -11,宽度= 212,高度= 278处绘制的,但正如您从屏幕截图中看到的那样,最终它的外观并非如此。

这就是为什么我在询问它是否在Graphics.DrawImage中出错。

enter image description here

编辑3: 我用Color.Red填充了m_pPngImage,这样我就可以看到它实际绘制的位置。这看起来很好,所以我猜问题不在CenterRect或类似的。

enter image description here

1 个答案:

答案 0 :(得分:0)

...伪代码

简单地说,给出一个矩形r

centerX = (r.Left + r.Right) / 2;
centerY = (r.Top  + r.Bottom) / 2;

以它的本地,井,中心为中心:

r.Left -= r.Width / 2;
r.Right -= r.Width /2;
r.Top -= r.Height / 2;
r.Bottom -= r.Height / 2;

真的,这就是它的全部。

<强>更新

根据您的更新/新屏幕截图,我确信字符单元格包含边距,或MeasureString正在添加它。无论哪种方式,您的输出都是正确的。

Also, you may find this thread helpful