实现给图片添加文字水印
通过给选定的图片添加文字水印,添加的文字从文本文档中获取,可以一次给相同的图片添加不同的文字水印,并以添加的文字命名图片;
Imports System.IOImports System.DrawingImports System.Drawing.Drawing2DPublic Class 给图片添加文字水印Dim MyImage As ImagePrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickOpenFileDialog1.Filter = "图像文件(JPeg,Gif,Bmp,etc.)|*.jpg;*.jpeg;*.gif; *.bmp; *.png| JPeg 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 文件 (*.gif)|*.gif |BMP 文件 (*.bmp)|*.bmp|Png 文件 (*.png)| *.png |所有文件(*.*)|*.*"If (OpenFileDialog1.ShowDialog() = DialogResult.OK) ThenLabel1.Text = OpenFileDialog1.FileNamePictureBox1.Image = Image.FromFile(Label1.Text)End IfEnd SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickIf (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) ThenLabel2.Text = FolderBrowserDialog1.SelectedPath + "\"End IfEnd SubSub draw1(ByVal str)MyImage = Image.FromFile(label1.text)Dim g = Graphics.FromImage(MyImage)Dim MyFont = New Font("宋体", 60)Dim MyBrush As New SolidBrush(Color.FromArgb(0, 0, 0))positingQuality = System.positingQuality.GammaCorrectedg.DrawString(str, MyFont, MyBrush, Int(MyImage.Width / 2), Int(MyImage.Height / 2))PictureBox1.Image = MyImageEnd SubPrivate Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.ClickTryDim filePath As String = "name.txt"If Label1.Text <> "原图片路径:" ThenIf Label2.Text <> "保存路径:" ThenDim fileLines() As String = IO.File.ReadAllLines(filePath, System.Text.Encoding.Default)For Each sql1 As String In fileLinesCall draw1(sql1)Dim savePath As String = Trim(Label2.Text) + sql1Call savePicture(savePath)MsgBox(savePath)PictureBox1.Refresh()NextLabel3.Text = "操作成功"ElseLabel3.Text = "没有选择保存路径"End IfElseLabel3.Text = "没有选择原图片路径"End IfCatch ex As ExceptionLabel3.Text = "当前路径下的name.txt文件不存在"End TryEnd SubSub savePicture(ByVal str)MyImage.Save("" & str & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)End SubEnd Class
截图
首先选择要添加水印的图片,指定一个保存路径,直接添加
可以一次完成所有任务