您的位置:首页 > 编程语言 > ASP

ASP.NET(VB) 给图片增加水印文字

2008-12-09 22:41 447 查看
'指定要添加水印文字的图片
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: