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

VS.net图片加水印的代码

2010-07-02 18:11 232 查看
前文:实在是好久没有更新这个博客了。主要是本人现在不走技术路线,过了那个研究技术的年代了。现在做一些零散的很多事情。

今天突然收到猎头的邮件,看着心里激动啊。呵呵。当然,目前的状况也就看看,毕竟人生的目标和要求已经变了。此一时彼一时

但还是TMD激动啊。。。

翻了翻自己的文件夹,最近实在是没有什么原创的东西,把这个很久以前做的图片加水印的程序share出来,我做了些小封装。不算完全原创。

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO

'code ex:
'Dim wm As New ImageDrawing.ImageModify
' thefile.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("1.jpg")) //thisfile is a file upload html control
'Dim imgoutput As Image = System.Drawing.Bitmap.FromFile(System.Web.HttpContext.Current.Server.MapPath("1.jpg"))

' With wm
' .WaterMarkImagePath = System.Web.HttpContext.Current.Server.MapPath("a.gif")
' .LucencyPercent = 10
' .ModifyImagePath = System.Web.HttpContext.Current.Server.MapPath("1.jpg")
' .WaterImagePosition = ImageDrawing.ImageModify.WaterImagePositionType.RightBottom
' .OutPath = System.Web.HttpContext.Current.Server.MapPath("2.jpg")
' .DrawImage()
' End With

Namespace ImageDrawing
'/// <summary>
'/// 图片修改类,主要是用来保护图片版权的
'/// </summary>

Public Class ImageModify

#Region "member fields"
Private mModifyImagePath As String = ""
Private mWaterMarkImagePath As String = ""
Private mWaterImagePosition As WaterImagePositionType
Private mRightSpace As Int32
Private mBottoamSpace As Int32
Private mLucencyPercent As Int32 = 50
Private mOutPath As String = ""
#End Region

'///设置水印在原图像中的位置
Public Enum WaterImagePositionType
LeftTop
RightTop
Center
LeftBottom
RightBottom
Value
End Enum

#Region "propertys"
'/// <summary>
'/// 获取或设置要修改的图像路径
'/// </summary>
Public Property ModifyImagePath() As String
Get
Return mModifyImagePath
End Get
Set(ByVal Value As String)
mModifyImagePath = Value
End Set
End Property

'/// <summary>
'/// 获取或设置在画的图片路径(水印图片)
'/// </summary>
Public Property WaterMarkImagePath() As String
Get
Return mWaterMarkImagePath
End Get
Set(ByVal Value As String)
mWaterMarkImagePath = Value
End Set
End Property

'///设置水印在原图像中的位置
Public Property WaterImagePosition() As WaterImagePositionType
Get
Return mWaterImagePosition
End Get
Set(ByVal Value As WaterImagePositionType)
mWaterImagePosition = Value
End Set
End Property

'/// <summary>
'/// 获取或设置水印在修改图片中的右边距
'/// </summary>
Public Property RightSpace() As Int32
Get
Return mRightSpace
End Get
Set(ByVal Value As Int32)
mRightSpace = Value
End Set
End Property

'//获取或设置水印在修改图片中距底部的高度
Public Property BottoamSpace() As Int32
Get
Return mBottoamSpace
End Get
Set(ByVal Value As Int32)
mBottoamSpace = Value
End Set
End Property

'/// <summary>
'/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
'/// </summary>
Public Property LucencyPercent() As Int32
Get
Return mLucencyPercent
End Get
Set(ByVal Value As Int32)
If Value >= 0 And Value <= 100 Then
mLucencyPercent = Value
Else
mLucencyPercent = 50
End If

End Set
End Property

'/// <summary>
'/// 获取或设置要输出图像的路径
'/// </summary>
Public Property OutPath() As String
Get
Return mOutPath
End Get
Set(ByVal Value As String)
mOutPath = Value
End Set
End Property

#End Region

#Region "methods"
'/// <summary>
'/// 开始绘制水印/图片水印,可设置透明度等
'/// </summary>
Public Function DrawImage() As Boolean

Dim bolReturn As Boolean = True

Dim modifyImage As Image = Nothing
Dim drawedImage As Image = Nothing
Dim g As Graphics = Nothing
Try
'//建立图形对象
modifyImage = Image.FromFile(Me.ModifyImagePath)
drawedImage = Image.FromFile(Me.WaterMarkImagePath)
g = Graphics.FromImage(modifyImage)
'//获取要绘制图形坐标
Dim x As Int32
Dim y As Int32
Select Case Me.WaterImagePosition
Case WaterImagePositionType.Center
x = (modifyImage.Width - drawedImage.Width) / 2
y = (modifyImage.Height - drawedImage.Height) / 2
Case WaterImagePositionType.LeftBottom
x = 0
y = modifyImage.Height - drawedImage.Height
Case WaterImagePositionType.LeftTop
x = 0
y = 0
Case WaterImagePositionType.RightBottom
x = modifyImage.Width - drawedImage.Width
y = modifyImage.Height - drawedImage.Height
Case WaterImagePositionType.RightTop
x = modifyImage.Width - drawedImage.Width
y = 0
Case WaterImagePositionType.Value
x = modifyImage.Width - Me.RightSpace
y = modifyImage.Height - Me.BottoamSpace
Case Else
x = 0
y = 0
End Select
If x < 0 Then
x = 0
End If
If y < 0 Then
y = 0
End If
'//设置颜色矩阵
Dim colorMatrix As ColorMatrix = New ColorMatrix(New 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, Convert.ToSingle(Me.LucencyPercent / 100.0F), 0}, _
New Single() {0, 0, 0, 0, 1}})

Dim imgAttr As ImageAttributes = New ImageAttributes
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
'//绘制阴影图像
g.DrawImage(drawedImage, New Rectangle(x, y, drawedImage.Width, drawedImage.Height), 0, 0, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr)
'//保存文件
Dim allowImageType As String() = {".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico"}
Dim file As FileInfo = New FileInfo(Me.ModifyImagePath)
Dim imageType As ImageFormat = ImageFormat.Gif
Select Case file.Extension.ToLower()
Case ".jpg"
imageType = ImageFormat.Jpeg
Case ".gif"
imageType = ImageFormat.Gif
Case ".png"
imageType = ImageFormat.Png
Case ".bmp"
imageType = ImageFormat.Bmp
Case ".tif"
imageType = ImageFormat.Tiff
Case ".wmf"
imageType = ImageFormat.Wmf
Case ".ico"
imageType = ImageFormat.Icon
Case Else
imageType = ImageFormat.Jpeg
End Select
Dim ms As MemoryStream = New MemoryStream()
modifyImage.Save(ms, imageType)
Dim imgData As Byte() = ms.ToArray()
Dim fs As FileStream = Nothing

If Not Me.OutPath.Equals(String.Empty) Then
fs = New FileStream(Me.OutPath, FileMode.Create, FileAccess.Write)
Else
fs = New FileStream(Me.ModifyImagePath, FileMode.Create, FileAccess.Write)
End If
If Not fs Is Nothing Then
fs.Write(imgData, 0, imgData.Length)
fs.Close()
End If
Catch ex As Exception
bolReturn = False
Finally

End Try
Return bolReturn

End Function

'修改图片大小
Public Function SizeChange(ByVal intWidth As Int32, ByVal intHeight As Int32) As Boolean
Dim bolReturn As Boolean = True
Try
Dim modifyImage As Image = Image.FromFile(Me.ModifyImagePath)
'//
Dim imageOut As Image = modifyImage.GetThumbnailImage(intWidth, intHeight, Nothing, System.IntPtr.Zero)
'//保存文件
Dim allowImageType As String() = {".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico"}
Dim file As FileInfo = New FileInfo(Me.ModifyImagePath)
Dim imageType As ImageFormat
Select Case file.Extension.ToLower()
Case ".jpg"
imageType = ImageFormat.Jpeg
Case ".gif"
imageType = ImageFormat.Gif
Case ".png"
imageType = ImageFormat.Png
Case ".bmp"
imageType = ImageFormat.Bmp
Case ".tif"
imageType = ImageFormat.Tiff
Case ".wmf"
imageType = ImageFormat.Wmf
Case ".ico"
imageType = ImageFormat.Icon
Case Else
imageType = ImageFormat.Jpeg
End Select
imageOut.Save(OutPath, imageType)
Catch ex As Exception
bolReturn = False
Finally

End Try
Return bolReturn

End Function
#End Region

#Region "new"
Public Sub New()
ModifyImagePath = ""
WaterMarkImagePath = ""
RightSpace = 0
BottoamSpace = 0
LucencyPercent = 50
OutPath = ""
mWaterImagePosition = WaterImagePositionType.LeftTop
End Sub

#End Region
End Class

End Namespace

调用部分:

Imports System.IO
Imports System.Drawing

Partial Class PicTest2
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'//thefile是个file field html 控件
thefile.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("BBS\SnkProductImages\1.jpg"))
Dim memstream As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim imgoutput As System.Drawing.Image = System.Drawing.Bitmap.FromFile(System.Web.HttpContext.Current.Server.MapPath("BBS\SnkProductImages\1.jpg"))
'//修改成80×80大小
Dim imgoutput2 As System.Drawing.Image = imgoutput.GetThumbnailImage(80, 80, Nothing, System.IntPtr.Zero)
imgoutput2.Save(System.Web.HttpContext.Current.Server.MapPath("BBS\SnkProductImages\2.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
Response.Write(thefile.PostedFile.FileName)
Response.Write("len:" + memstream.length.tostring())
imgoutput.Dispose()
imgoutput2.Dispose()
Response.Write("上传成功!")
Response.Write(System.Web.HttpContext.Current.Server.MapPath("BBS\SnkProductImages\2.jpg"))

'//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
Dim g As Graphics = Graphics.FromImage(imgoutput)
g.DrawImage(imgoutput, 0, 0, imgoutput.Width, imgoutput.Height)
Dim f As Font = New Font("Verdana", 32)
Dim b As Brush = New SolidBrush(Color.Red)
Dim addText As String = "SNK Star"
g.DrawString(addText, f, b, 10, 10)
g.Dispose()

imgoutput.Save(System.Web.HttpContext.Current.Server.MapPath("BBS\SnkProductImages\3.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)

End Sub

End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: