图片水印有两种方法,一是读取水印的所有像素颜色,颜色上加上设置的alpha值,然后将每个像素绘制到指定的位置;二是使用颜色矩阵(
如果忘记了如何使用颜色矩阵的朋友,可以参看《
vb.net 教程 5-16 图像处理之ImageAttributes 类1 颜色矩阵1
》开始的内容),这也是本节所采用的方法。
设计界面,这里测试使用了一个ie的图标作为图片水印:
具体代码:
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click Dim logoOpacity As Single = HScrollBar1.Value / 255 Dim logoImg As Bitmap = PictureBox2.Image Dim logoLeft As Integer = 10 Dim logoTop As Integer = 10 Dim imgDest As Bitmap = PictureBox1.Image.Clone Dim g As Graphics = Graphics.FromImage(imgDest) Dim matrixArray()() As Single = { New Single() {1,0},New Single() {0,1,logoOpacity,1}} Dim cMatrix As New Imaging.ColorMatrix(matrixArray) Dim imgAttr As New Imaging.ImageAttributes() imgAttr.SetColorMatrix(cMatrix,Imaging.ColorMatrixFlag.Default,Imaging.ColorAdjustType.Bitmap) Dim recDest As New Rectangle(logoLeft,logoTop,logoImg.Width,logoImg.Height) g.DrawImage(logoImg,recDest,logoImg.Height,GraphicsUnit.Pixel,imgAttr) g.Dispose() PictureBox3.Image = imgDest End Sub
下面解释一下代码:
运行结果: