我已将给定字符串转换为JPG文件格式。但我需要中心的那些图像串。我从矩形的左侧获得输出。
Dim stext As String = "Testing"
Dim format As StringFormat = New StringFormat()
Dim MyRect As Rectangle = New Rectangle(0, 0, 400, 800)
Dim MyGraphics As Graphics = Me.CreateGraphics()
Dim MyImg As Image = New Bitmap(391, 132, MyGraphics)
Dim imageGraphics As Graphics = Graphics.FromImage(MyImg)
imageGraphics.FillRectangle(Brushes.White, MyRect)
format.Alignment = StringAlignment.Center
format.LineAli`enter code here`gnment = StringAlignment.Center
imageGraphics.DrawString("Testing", New Font("Times New Roman", 30, Drawing.FontStyle.Bold), Brushes.Black, RectangleF.op_Implicit(MyRect))
MyGraphics.DrawImage(MyImg, MyRect)
MyImg.Save(Destfilename & "/" & "test.jpg")
我已经给出了这个代码输出。但我需要在中心位置中使用此字符串。怎么做,请帮助我度过难关。 输出:https://drive.google.com/file/d/0B_nzYHWVJJ7Ka3N0V2NmRnl3UFk/view?usp=sharing
答案 0 :(得分:0)
我希望这个例子可以帮助你
Private Sub CenterTextAt(ByVal gr As Graphics, ByVal txt As _
String, ByVal x As Single, ByVal y As Single)
' Mark the center for debugging.
gr.DrawLine(Pens.Red, x - 10, y, x + 10, y)
gr.DrawLine(Pens.Red, x, y - 10, x, y + 10)
' Make a StringFormat object that centers.
Dim sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
' Draw the text.
gr.DrawString(txt, Me.Font, Brushes.Black, x, y, sf)
sf.Dispose()
End Sub