如何自动对齐和调整 VB.net 中转换为图像的文本

How to auto align and adjust text in VB.net that getting converted to Image

我正在尝试使用 Vb.net 将文本转换为图像的代码,我的问题是我需要通过文本框接受来自用户的消息(最多 160 个字符),我需要将其转换为图像。生成的图像应居中对齐,图像最大分辨率应为 800x600。

因此,如果需要,消息应该在新行中整齐对齐,并且完全对齐到中间。

我正在尝试的代码如下:

============================================= =========

尝试

    Dim Text As String = TextBox3.Text

    Dim FontColor As Color = Color.Blue

    Dim BackColor As Color = Color.White

    Dim FontName As String = "Times New Roman"

        Dim FontSize As Integer = 36


        Dim Height As Integer = 60

        Dim Width As Integer = 200

    Dim daten As String
    daten = Now.ToString("ddMMyyyyhhmmss")
    Dim FileName As String = daten
    Dim objBitmap As New Bitmap(Width, Height)
    Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
    Dim objColor As Color
    objColor = Nothing

    Dim objFont As New Font(FontName, FontSize)

    'Following PointF object defines where the text will be displayed in the

    'specified area of the image

    Dim objPoint As New PointF(5.0F, 5.0F)
    Dim objBrushForeColor As New SolidBrush(FontColor)

    Dim objBrushBackColor As New SolidBrush(BackColor)
    objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)
    objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint)
    objBitmap.Save("D:\DNB\" + daten + ".JPG", ImageFormat.Jpeg)
        PictureBox1.Image = Image.FromFile("D:\DNB\" + daten + ".JPG")
    Catch ex As Exception

    End Try

您是否尝试过 Graphics 对象的 MeasureString 函数及其各种覆盖?有了它,您可以测量 space 给定大小和字体的文本在屏幕上需要多少。有了这些知识,您就可以计算左上角的点来使文本居中显示。