水印的制作功能分为两大部分:
第一是水印库的管理:包括水印库列表,水印的新建,水印的编辑和水印的删除和激活管理。
第二是上传文件添加水印图片的过程。
首先我们来了解一下水印库的管理功能。
1.设计水印数据库表
最后一部分就是上传图片水印的处理。
Dim ImgSourcePhoto As Image = Image.FromFile(SaveToLocation & NewFileName)
Dim WaterFilePath As String = IO.Path.Combine(SWRequestCache.CMSSettings.RootFolderPath, Schoolwires.CMS.WatermarkGallery.WatermarkGallery.AssetPath & WatermarkID & "/" & WatermarkFile)
Dim imgWatermark As Image = Image.FromFile(WaterFilePath)
If ImgSourcePhoto.Width > imgWatermark.Width AndAlso ImgSourcePhoto.Height > imgWatermark.Height Then
Dim phWidth As Integer = ImgSourcePhoto.Width
Dim phHeight As Integer = ImgSourcePhoto.Height
Dim bmPhoto As Bitmap = New Bitmap(phWidth, phHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(ImgSourcePhoto.HorizontalResolution, ImgSourcePhoto.VerticalResolution)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
Dim locationArr As ArrayList = GetLocation(Location, ImgSourcePhoto, imgWatermark)
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
grPhoto.Clear(Color.Transparent)
grPhoto.DrawImage(ImgSourcePhoto, New Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel)
Dim bmWatermark As Bitmap = New Bitmap(bmPhoto)
bmWatermark.SetResolution(ImgSourcePhoto.HorizontalResolution, ImgSourcePhoto.VerticalResolution)
Dim grWatermark As Graphics = Graphics.FromImage(bmWatermark)
Dim imageAttributes As ImageAttributes = New ImageAttributes
Dim colorMap As ColorMap = New ColorMap
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0)
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0)
Dim remapTable() As ColorMap = {colorMap}
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)
Dim colorMatrixElements()() As Single = {New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, New Single() {0.0F, 0.0F, 0.0F, 0.5F, 0.0F}, New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}}
Dim wmColorMatrix As ColorMatrix = New ColorMatrix(colorMatrixElements)
imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
Dim arrLocation As ArrayList = GetLocation(Location, ImgSourcePhoto, imgWatermark)
grWatermark.DrawImage(imgWatermark, New Rectangle(CInt(arrLocation(0)), CInt(arrLocation(1)), wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes)
ImgSourcePhoto.Dispose()
ImgSourcePhoto = bmWatermark
grPhoto.Dispose()
grWatermark.Dispose()
ImgSourcePhoto.Save(SaveToLocation & NewFileName)
ImgSourcePhoto.Dispose()
bmWatermark = Nothing
Else
ImgSourcePhoto.Dispose()
End If
这样,水印就贴上去了,中间下面就是水印了。