2017-10-16 73 views
-2

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

因此,如果需要,消息应该整齐排列在新行中,并完美地与中间对齐。

我想要的代码如下:

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

尝试

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 
+0

请参阅:[如何避免滥用标签?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) – EJoshuaS

回答

0

您是否尝试过的Graphics对象的MeasureString功能和它的各种替代?通过这种方式,您可以测量屏幕上给定大小和字体所需的文本大小。有了这些知识,您可以计算左上角的点来使文本显示居中。