如何制作上传图片的水印

水印的制作功能分为两大部分:

第一是水印库的管理:包括水印库列表,水印的新建,水印的编辑和水印的删除和激活管理。

第二是上传文件添加水印图片的过程。

首先我们来了解一下水印库的管理功能。

1.设计水印数据库表

增加表:  Watermark
字段列表:
 
WatermarkID: PK ,int, not null
OriginalFile:   nvarchar(100), not null   -->  该值保存用户上传的图片的原始名称。 
FileName:   nvarchar(100), not null   --> 以本条记录的ID做为该图片的文件名进而重新命名该文件。
WatermarkFile:   nvarchar(100), not null   -->  水印图片名字。以guid值命名。
Width: int, not null
Height:   int, not null
Location: int, not null    -->  1 - 左上角;  2  - 右上角;  3 - 中间; 4 - 左下角; 5 - 右下角
Spin:int, not null:   存储图片翻转的角度。
Transparency: int, not null: 存储图片的透明度。
CreateDate: datetime, not null
ModifiedDate: datetime, not null
ModifiedByID: int , not null
Active: bit, not null
 
存储过程:
WatermarkList,  WatermarkSelectByWatermarkID, WatermarkInsert, WatermarkUpdate, WatermarkDelete
 
2.新建和编辑水印设置
 
以上功能全部用javascript脚本语言来实现,图片旋转和透明度采用了jquery插件来控制。
 
3.水印库的管理
该部分主要分为水印的激活和删除功能。
 

最后一部分就是上传图片水印的处理。

  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

这样,水印就贴上去了,中间下面就是水印了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值