Imports System.IO
Imports System.Drawing
Imports System.web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System
Public Class uploadimg
' filePath是客户文件的完全路径,savepath是保存在服务器的文件夹名,imgname是文件更名后的名字,包括后缀名,size是图片缩微的实际大小
'这是缩微图形成
Public Sub uploadSmallImg(ByVal filePath As String, ByVal savepath As String, ByVal ImgName As String, ByVal size As Int16)
Dim Img As System.Drawing.Image = System.Drawing.Image.FromFile(filePath)
Dim ImgOutput As Bitmap
Dim neww, newh As Integer '考虑压缩失真问题,按比例保存
If Img.Height <= Size And Img.Width <= Size Then '直接保存
neww = Img.Height
newh = Img.Width
ElseIf Img.Width > Img.Height Then '按照width的比例保存
neww = Size
newh = Size * Img.Height / Img.Width
Else '按照高度比例保存
newh = Size
neww = Size * Img.Width / Img.Height
End If
ImgOutput = New Bitmap(Img, neww, newh) '形成缩微图
Img.Dispose()
'图片上传到服务器文件
Call savetofolder(ImgOutput, savepath + "s" + ImgName.ToLower)
End Sub
'保存原图,没有添加别的东西
' filePath是文件的完全路径,savepath是保存在服务器的文件夹名,imgname是文件更名后的名字,包括后缀名
Public Sub uploadbigimg(ByVal filepath As String, ByVal savepath As String, ByVal imgname As String)
Dim imgoutput As Bitmap = New Bitmap(filepath)
'图片上传到服务器文件
Call savetofolder(imgoutput, savepath + imgname.ToLower)
End Sub
' 由时间形成的新的文件名称,包括了后缀名称,精确到毫秒的文件名称
' 因为jpg的图片最小,都考虑用jpg的格式
Public Function newfilename() As String
'精确到毫秒的文件名称,包括了后缀了, 以防上传文件同名
Dim nowtime As DateTime = DateTime.Now
Dim changefilename As String = nowtime.ToString("yyyyMMddHHmmss") + nowtime.Millisecond.ToString + ".jpg" '+ ImageExtensionName
Return (changefilename.ToString)
End Function
'添加图片水印字符,然后上传
' filePath是客户文件的完全路径,savepath是保存在服务器的文件夹名,imgname是文件更名后的名字,包括后缀名,addstr是要填加的水印内容
Public Sub addwatermark(ByVal filepath As String, ByVal savepath As String, ByVal imgname As String) ', ByVal addstr As String)
'转换为位图
Dim bmp As Bitmap = New Bitmap(filepath)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim fontsize As Int16
Dim addstr As String
'考虑图的大小,定义水印字体的大小
If bmp.Width < 120 Or bmp.Height < 120 Then
fontsize = 10
addstr = "--Cyt--" '图片太小添加--Cyt--
Else : fontsize = 20
addstr = "www.Caiyt.com" '图片比较大时添加www.Caiyt.com
End If
'定义画笔和字体
Dim f As Font = New Font("Arial Black", fontsize, FontStyle.Italic)
Dim b As Brush = New SolidBrush(Color.FromArgb(198, Color.Black)) '透明度100%
'添加水印,取中间值添加
g.DrawString(addstr, f, b, bmp.Width / 2, bmp.Height / 2)
'保存添加了水印的图片,选择保存格式
Call savetofolder(bmp, savepath + imgname.ToLower)
g.Dispose()
End Sub
'添加水印图片,上传
Public Sub addwatermarkimg(ByVal filepath As String, ByVal savepath As String, ByVal imgname As String, ByVal watermarkpath As String)
Dim bmp As Bitmap = New Bitmap(filepath)
Dim g As Graphics = Graphics.FromImage(bmp)
'考虑图的大小,定义水印字体的大小
If bmp.Width < 120 Or bmp.Height < 120 Then
Dim fontsize As Int16
Dim addstr As String
fontsize = 10
addstr = "--Cyt--" '图片太小添加--Cyt--,不添加图片
Dim f As Font = New Font("Arial Black", fontsize, FontStyle.Italic)
Dim b As Brush = New SolidBrush(Color.FromArgb(198, Color.Silver)) '透明度100%
'添加水印,取中间值添加
g.DrawString(addstr, f, b, bmp.Width / 2, bmp.Height / 2)
'图片比较大时添加图片
Else
Dim markimg As Bitmap = New Bitmap(watermarkpath)
'Rectangle(X,Y,width,height)
'放在图片的左上角
g.DrawImage(markimg, New Rectangle(10, 10, markimg.Width, markimg.Height), 0, 0, markimg.Width, markimg.Height, GraphicsUnit.Pixel)
End If
'定义画笔和字体
'保存添加了水印的图片,选择保存格式
Call savetofolder(bmp, savepath + imgname.ToLower)
g.Dispose()
End Sub
Public Sub savetofolder(ByVal bmp As Bitmap, ByVal path As String)
'按照给定的路径保存,统一按照jpg的格式保存
'必须要求有上载的文件的格式,要不然不知道服务器是什么
bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg)
bmp.Dispose()
End Sub
End Class