我正在尝试使用 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
函数及其各种覆盖?有了它,您可以测量给定大小和字体的文本在屏幕上需要多少空间。有了这些知识,您就可以计算左上角的点,用于使文本显示为居中。