📄 watermark.aspx.vb
字号:
Imports System
Imports System.IO
Imports System.Drawing
Public Class WaterMark
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents Image1 As System.Web.UI.WebControls.Image
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Page.IsPostBack = False Then
Image1.ImageUrl = "mikepp.gif"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If File1.PostedFile.FileName.Trim() <> "" Then
'上传文件
Dim extension As String = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower()
Dim fileName As String = DateTime.Now.ToString("yyyyMMddhhmmss")
Dim path As String = Server.MapPath(".") + "/Downloads/" + fileName + extension
File1.PostedFile.SaveAs(Path)
'加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
Dim image As Image = System.Drawing.Image.FromFile(path)
Dim g As Graphics = Graphics.FromImage(image)
g.DrawImage(image, 10, 10, image.Width, image.Height)
Dim f As Font = New Font("Verdana", 12)
Dim b As Brush = New SolidBrush(Color.Red)
Dim addText As String = "3One工作室(http://www.oneoneone.net)"
g.DrawString(addText, f, b, 10, 10)
g.Dispose()
'保存加水印过后的图片,删除原始图片
Dim newPath As String = Server.MapPath(".") + "/Downloads/" + fileName + "_new" + extension
image.Save(newPath)
image.Dispose()
If File.Exists(path) Then
File.Delete(path)
End If
Image1.ImageUrl = newPath
'Response.Redirect(newPath)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'上传文件
Dim extension As String = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToUpper()
Dim fileName As String = DateTime.Now.ToString("yyyyMMddhhmmss")
Dim path As String = Server.MapPath(".") + "/Downloads/" + fileName + extension
File1.PostedFile.SaveAs(Path)
'加图片水印
Dim Image As Image = System.Drawing.Image.FromFile(path)
Dim copyImage As Image = System.Drawing.Image.FromFile(Server.MapPath(".") + "/Downloads/logo.png")
Dim g As Graphics = Graphics.FromImage(Image)
g.DrawImage(copyImage, New Rectangle(Image.Width - copyImage.Width, Image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel)
g.Dispose()
'保存加水印过后的图片,删除原始图片
Dim newPath As String = Server.MapPath(".") + "/Downloads/" + fileName + "_new" + extension
Image.Save(newPath)
Image.Dispose()
If File.Exists(path) Then
File.Delete(path)
End If
Image1.ImageUrl = newPath
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -