watermarkcontroller.vb

来自「C#语言制作asp.net网上商店的」· VB 代码 · 共 367 行

VB
367
字号
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Drawing.Drawing2D
Imports System.Web
Imports System.Net

Namespace NetShopForge.Common.nsfImage

    Public Class WaterMarkController

        Public Enum MarkType
            [Text]
            Image
        End Enum 'MarkType

#Region "---成员变量---"
        Private _text As String = ""
        Private _imgPath As String = ""
        Private _markX As Integer = 0
        Private _markY As Integer = 0
        Private _transparency As Single = 1
        Private _fontFamily As String = "宋体"
        Private _textColor As Color = Color.Black
        Private _textbold As Boolean = False
        Private sizes() As Integer = {48, 32, 16, 8, 6, 4}
        Private _image As Image = Nothing
        Private _markedIamge As Image = Nothing
        Private _markType As MarkType = MarkType.Text
#End Region

#Region "---构造函数---"
        '''<summary>实例化一个水印类
        ''' </summary>
        Public Sub New()
        End Sub 'New


        '
        ''' <summary>
        ''' 初始化一个只添加文字水印得实例
        ''' </summary>
        ''' <param name="text">水印文字</param>
        ''' <param name="fontFamily">文字字体</param>
        ''' <param name="bold">是否粗体</param>
        ''' <param name="color">字体颜色</param>
        ''' <param name="markX">标记位置横坐标</param>
        ''' <param name="markY">标记位置纵坐标</param>
        Public Sub New(ByVal SourceImage As Image, ByVal [text] As String, ByVal fontFamily As String, ByVal bold As Boolean, ByVal color As Color, ByVal markX As Integer, ByVal markY As Integer)
            Me._markType = MarkType.Text
            Me._text = [text]
            Me._fontFamily = fontFamily
            Me._textbold = bold
            Me._textColor = color
            Me._markX = markX
            Me._markY = markY
            Me._image = SourceImage
            Me.Mark()
        End Sub 'New

        '
        ''' <summary>
        ''' 实例化一个只添加图片水印得实例
        ''' </summary>
        ''' <param name="imagePath">水印图片路径</param>
        ''' <param name="tranparence">透明度</param>
        ''' <param name="markX">标记位置横坐标</param>
        ''' <param name="markY">标记位置纵坐标</param>
        Public Sub New(ByVal SourceImage As Image, ByVal imagePath As String, ByVal tranparence As Single, ByVal markX As Integer, ByVal markY As Integer)
            Me._markType = MarkType.Image
            Me._imgPath = imagePath
            Me._markX = markX
            Me._markY = markY
            Me._transparency = tranparence
            Me._image = SourceImage
            Me.Mark()
        End Sub 'New
        '
#End Region

#Region "----公共属性----"

        ''' <summary>水印类型
        ''' </summary>
        Public Property WaterMarkType() As MarkType
            Get
                Return _markType
            End Get
            Set(ByVal Value As MarkType)
                _markType = Value
            End Set
        End Property
        '
        ''' <summary>
        ''' 文字水印得内容
        ''' </summary>
        Public Property [Text]() As String
            Get
                Return _text
            End Get
            Set(ByVal Value As String)
                _text = Value
            End Set
        End Property '
        ''' <summary>
        ''' 水印图片得路径
        ''' </summary>
        Public Property WaterImagePath() As String
            Get
                Return _imgPath
            End Get
            Set(ByVal Value As String)
                Me._imgPath = Value
            End Set
        End Property
        '
        ''' <summary>
        ''' 水印图片
        ''' </summary>
        Public ReadOnly Property WaterImage() As Image
            Get
                Try
                    Return Image.FromFile(Me.WaterImagePath)
                Catch
                End Try
                Return Nothing
            End Get
        End Property
        '
        ''' <summary>
        ''' 添加水印位置得横坐标
        ''' </summary>
        Public Property MarkX() As Integer
            Get
                Return _markX
            End Get
            Set(ByVal Value As Integer)
                _markX = Value
            End Set
        End Property '

        ''' <summary>
        ''' 添加水印位置得纵坐标
        ''' </summary>
        Public Property MarkY() As Integer
            Get
                Return _markY
            End Get
            Set(ByVal Value As Integer)
                _markY = Value
            End Set
        End Property '

        ''' <summary>
        ''' 水印得透明度
        ''' </summary>
        Public Property Transparency() As Single
            Get
                If _transparency > 1.0F Then
                    _transparency = 1.0F
                End If
                Return _transparency
            End Get
            Set(ByVal Value As Single)
                _transparency = Value
            End Set
        End Property '

        ''' <summary>
        ''' 水印文字得颜色
        ''' </summary>
        Public Property TextColor() As Color
            Get
                Return _textColor
            End Get
            Set(ByVal Value As Color)
                _textColor = Value
            End Set
        End Property '

        ''' <summary>
        ''' 水印文字得字体
        ''' </summary>
        Public Property TextFontFamilyStr() As String
            Get
                Return _fontFamily
            End Get
            Set(ByVal Value As String)
                _fontFamily = Value
            End Set
        End Property

        Public ReadOnly Property TextFontFamily() As FontFamily
            Get
                Return New FontFamily(Me.TextFontFamilyStr)
            End Get
        End Property '

        ''' <summary>
        ''' 水印文字是否加粗
        ''' </summary>
        Public Property Bold() As Boolean
            Get
                Return _textbold
            End Get
            Set(ByVal Value As Boolean)
                _textbold = Value
            End Set
        End Property '

        ''' <summary>
        ''' 原图
        ''' </summary>
        Public Property SourceImage() As Image
            Get
                Return _image
            End Get
            Set(ByVal Value As Image)
                _image = Value
            End Set
        End Property

        ''' <summary>
        ''' 加过水印之后得图
        ''' </summary>
        Public ReadOnly Property MarkedImage() As Image
            Get
                Return Me._markedIamge
            End Get
        End Property
#End Region

#Region "----方法事件---"
        Public Overloads Function Mark(ByVal img As Image, ByVal markType As MarkType, ByVal [text] As String, ByVal waterImg As Image, ByVal markx As Integer, ByVal marky As Integer, ByVal bold As Boolean, ByVal textColor As Color, ByVal transparence As Single, ByVal fontFamily As FontFamily) As Image

            '首先先判断该图片是否是 gif动画,如果为gif动画不对图片进行改动
            Dim guid As Guid
            For Each guid In img.FrameDimensionsList
                Dim dimension As New FrameDimension(guid)
                If img.GetFrameCount(dimension) > 1 Then
                    Return img
                End If
            Next guid
            Try
                '添加文字水印
                If markType = markType.Text Then
                    '根据源图片生成新的Bitmap对象作为作图区,为了给gif图片添加水印,才有此周折
                    Dim newBitmap As New Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb)
                    '设置新建位图得分辨率
                    newBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution)
                    '创建Graphics对象,以对该位图进行操作
                    Dim g As Graphics = Graphics.FromImage(newBitmap)
                    '消除锯齿
                    g.SmoothingMode = SmoothingMode.AntiAlias
                    '将原图拷贝到作图区
                    g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
                    '声明字体对象
                    Dim cFont As Font = Nothing
                    '用来测试水印文本长度得尺子
                    Dim size As New SizeF
                    '探测出一个适合图片大小得字体大小,以适应水印文字大小得自适应
                    Dim i As Integer
                    For i = 0 To 5
                        '创建一个字体对象
                        cFont = New Font(fontFamily, sizes(i))
                        '是否加粗
                        If Not bold Then
                            cFont = New Font(fontFamily, sizes(i), FontStyle.Regular)
                        Else
                            cFont = New Font(fontFamily, sizes(i), FontStyle.Bold)
                        End If
                        '测量文本大小
                        size = g.MeasureString(Me.Text, cFont)
                        '匹配第一个符合要求得字体大小
                        If CType(size.Width, Integer) < CType(img.Width, Integer) Then
                            'ToDo: Unsigned Integers not supported
                            'ToDo: Unsigned Integers not supported
                            Exit For
                        End If
                    Next i
                    '创建刷子对象,准备给图片写上文字
                    Dim brush = New SolidBrush(textColor)
                    '在指定得位置写上文字
                    g.DrawString([text], cFont, brush, markx, marky)
                    '释放Graphics对象
                    g.Dispose()
                    '将生成得图片读入MemoryStream
                    Dim ms As New System.IO.MemoryStream
                    newBitmap.Save(ms, ImageFormat.Jpeg)
                    '重新生成Image对象
                    img = System.Drawing.Image.FromStream(ms)
                    '返回新的Image对象
                    Return img

                    '添加图像水印
                Else
                    If markType = markType.Image Then
                        '获得水印图像
                        Dim markImg As Image = waterImg
                        '创建颜色矩阵
                        Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, transparence, 0}, New Single() {0, 0, 0, 0, 1}}
                        '注意:此处为0.0f为完全透明,1.0f为完全不透明
                        Dim colorMatrix As New ColorMatrix(ptsArray)
                        '新建一个Image属性
                        Dim imageAttributes As New ImageAttributes
                        '将颜色矩阵添加到属性
                        imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default)
                        '生成位图作图区
                        Dim newBitmap As New Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb)
                        '设置分辨率
                        newBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution)
                        '创建Graphics
                        Dim g As Graphics = Graphics.FromImage(newBitmap)
                        '消除锯齿
                        g.SmoothingMode = SmoothingMode.AntiAlias
                        '拷贝原图到作图区
                        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
                        '如果原图过小
                        If markImg.Width > img.Width Or markImg.Height > img.Height Then
                            Dim callb As System.Drawing.Image.GetThumbnailImageAbort = Nothing
                            '对水印图片生成缩略图,缩小到原图得1/4
                            Dim new_img As System.Drawing.Image = markImg.GetThumbnailImage(img.Width / 4, markImg.Height * img.Width / markImg.Width, callb, New System.IntPtr)
                            '添加水印
                            g.DrawImage(new_img, New Rectangle(markx, marky, new_img.Width, new_img.Height), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imageAttributes)
                            '释放缩略图
                            new_img.Dispose()
                            '释放Graphics
                            g.Dispose()
                            '将生成得图片读入MemoryStream
                            Dim ms As New System.IO.MemoryStream
                            newBitmap.Save(ms, ImageFormat.Jpeg)
                            '返回新的Image对象
                            img = Image.FromStream(ms)
                            Return img
                            '原图足够大
                        Else
                            '添加水印
                            g.DrawImage(markImg, New Rectangle(markx, marky, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes)
                            '释放Graphics
                            g.Dispose()
                            '将生成得图片读入MemoryStream
                            Dim ms As New System.IO.MemoryStream
                            newBitmap.Save(ms, ImageFormat.Jpeg)
                            '返回新的Image对象
                            img = Image.FromStream(ms)
                            Return img
                        End If
                    End If
                End If
                Return img
            Catch
            End Try
            Return Nothing
        End Function 'Mark

        '''<summary>
        '''添加水印,此方法适用于gif格式得图片
        '''</summary>
        Private Overloads Sub Mark()
            Me._markedIamge = Mark(Me.SourceImage, Me.WaterMarkType, Me._text, Me.WaterImage, Me._markX, Me._markY, Me._textbold, Me._textColor, Me._transparency, Me.TextFontFamily)
        End Sub 'Mark
#End Region

    End Class

End Namespace

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?