'指定要添加浮水印文字的圖片
Dim img As Image = Image.FromFile(Server.MapPath("~/upload/1.jpg"))
'建立一個位元影像對象以供繪圖操作(不能直接操作 BMP 以外的格式,可能吧)
Dim bitmap As New Bitmap(343, 498)
'要繪製文字的字型
Dim f As New Font("微軟雅黑", 16, FontStyle.Underline)
'文字內容
Dim str As String = TextBox5.Text
'建立一個繪製映像的對象
Dim g As Graphics = Graphics.FromImage(bitmap)
'線條的座標
Dim x1, x2, y1, y2, s1, s2 As Integer
x1 = CType(TextBox1.Text, Integer)
x2 = CType(TextBox2.Text, Integer)
y1 = CType(TextBox3.Text, Integer)
y2 = CType(TextBox4.Text, Integer)
'文字的座標
s1 = CType(TextBox6.Text, Integer)
s2 = CType(TextBox7.Text, Integer)
'限定繪圖的範圍
g.DrawImage(img, 0, 0, 343, 498)
'繪製文字
g.DrawString(str, f, Brushes.Red, s1, s2)
'繪製線條
g.DrawLine(Pens.White, x1, x2, y1, y2)
'繪製完成後的儲存路徑
Dim iimg As String = "~/upload/11.jpg"
Dim img1 As String = Server.MapPath(iimg)
'JPG 映像品質參數
Dim ep As Imaging.EncoderParameters = New Imaging.EncoderParameters
'映像品質值
ep.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, CLng(TextBox8.Text))
'以流方式讀取映像內容
Dim codecs As Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageEncoders
Dim ici As Imaging.ImageCodecInfo
For Each codec As Imaging.ImageCodecInfo In codecs
If codec.MimeType = "image/jpeg" Then
ici = codec
End If
Next
'儲存繪製好的映像
bitmap.Save(img1, ici, ep)
Image1.ImageUrl = iimg
'釋放對象
g.Dispose()
bitmap.Dispose()
img.Dispose()