2015-04-21 59 views
1

我正在將圖像加載到圖片框和繪製som文本。 當我拖動它看起來很大的文字,但當我拉它或鎖定它的地方它變得太小。圖片框sizemode設置爲縮放。所以巨大的圖像變得很小。如何在picturebox中設置正確的字體大小和預覽?當抽取字符串時字體大小過小

子btnLock負責「鎖定」文本到影像 和 子Picturebox1_Paint預覽

我怎麼可以讓預覽縮小字體大小或使btnLock比例放大字號

Dim Loc As Point 
    Dim Pxy As Point 

    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove 
     txtButton.Text = MouseButtons.Left 
     If e.Button = MouseButtons.Left Then 
      Loc = e.Location 
      Me.PictureBox1.Invalidate() 
     End If 
    End Sub 

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint 
     Dim myFontSize As Integer 
     Integer.TryParse(ComboBox1.Text, myFontSize) 
     Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic) 
     Dim pt As Point = PictureBox1.PointToClient(Control.MousePosition) 
     e.Graphics.DrawString(txtA.Text, myFont, Brushes.Black, pt.X, pt.Y) 
    End Sub 

    Private Sub btnLock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLock.Click 
     Dim myFontSize As Integer 
     Integer.TryParse(ComboBox1.Text, myFontSize) 
     Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic) 
     Dim g As Graphics = Graphics.FromImage(Me.PictureBox1.Image) 
     ' g.DrawString(txtA.Text, myFont, Brushes.Black, Loc.X, Loc.Y) 
     g.DrawString(txtA.Text, myFont, Brushes.Black, Pxy.X, Pxy.Y) 
     ' Me.PictureBox1.Invalidate() 
    End Sub 

感謝您幫助!-DB35M

+0

你爲什麼不爲** ** myFontSize整數定義的值? – MacKentoch

+0

@MacKentoch我正在使用Combobox1來更改字體大小 – db35m

+0

好吧,我會調試並添加斷點以檢查發生了什麼。我想你已經做到了。 – MacKentoch

回答

0

我想你會得到這種行爲,因爲兩個Graphics對象有不同的大小。你的對象g正在從你的圖片框的圖片的邊界(沒有縮放)和你的圖形對象PictureBox1越來越接近圖片框容器。這意味着它們會有不同的尺寸,因此myFontSize看起來會有不同的尺寸。

試圖根據圖像的縮小比例「縮小」字體大小實際上很難完成。取而代之的是,解決通過更改SizeModeNormal和調整大小的容器以適應圖像的問題:

PictureBox1.SizeMode = PictureBoxSizeMode.Normal