2015-03-02 30 views
0

我已將給定的字符串轉換爲JPG文件格式。但我需要這些圖像字符串在中心。 Iam從矩形的左側獲得輸出。如何從給定字符串創建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

我希望這個例子可以幫助你

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

感謝您的解決方案,但什麼樣的價值觀,我們應該提供 – user2944173 2015-03-02 11:49:45

+0

你惠康x和y的值。 x e y是繪製文本的座標。 [鏈接](https://msdn.microsoft.com/en-us/library/d3thd43b(V = vs.110)的.aspx) – GMazzacua 2015-03-03 11:59:52