-2

我正在尝试使用 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
4

1 回答 1

0

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

于 2017-10-17T05:44:00.980 回答