技術(shù) 點
- 技術(shù)
- 點
- V幣
- 點
- 積分
- 13311
|
-
-
- Public Sub AddTextToImg(ByVal file As String, ByVal newFile As String, ByVal text As String)
- If Not System.IO.File.Exists(file) Then
- Throw New System.IO.FileNotFoundException("文件不存在。")
- End If
- If text = String.Empty Then
- Return
- End If
- '還需要判斷文件類型是否為圖像類型,這里就不贅述了
- Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(file)
- Dim bitmap As New System.Drawing.Bitmap(image, image.Width, image.Height)
- Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bitmap)
- Dim fontSize As Single = 12.0F '字體大小
- Dim textWidth As Single = text.Length * fontSize '文本的長度
- '下面定義一個矩形區(qū)域,以后在這個矩形里畫上白底黑字
- Dim rectX As Single = 0
- Dim rectY As Single = 0
- Dim rectWidth As Single = text.Length * (fontSize + 8)
- Dim rectHeight As Single = fontSize + 8
- '聲明矩形域
- Dim textArea As New System.Drawing.RectangleF(rectX, rectY, rectWidth, rectHeight)
- Dim font As New System.Drawing.Font("宋體", fontSize) '定義字體
- Dim whiteBrush As New System.Drawing.SolidBrush(Color.White) '白筆刷,畫文字用
- Dim blackBrush As New System.Drawing.SolidBrush(Color.Black) '黑筆刷,畫背景用
- g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight)
- g.DrawString(text, font, whiteBrush, textArea)
- '保存為Jpg類型
- bitmap.Save(newFile, System.Drawing.Imaging.ImageFormat.Jpeg)
- g.Dispose()
- bitmap.Dispose()
- image.Dispose()
- End Sub
-
-
復制代碼 |
|