|
-
-
- 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 '文本的长度
- '下面定义一个矩形区域,以后在这个矩形里画上白底黑字
- 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
-
-
复制代码 |
|