Public Class uploadimg
Public file As Object '输入图片的文件框
Public Maximg As String '大图片的存放地址
Public Minimg As String '小图片的存放地址
Public Msglab As Label '错误报告的标签框
Public x As Integer '缩略图的宽
Public y As Integer '缩略图的高
Dim s1 As String '图片的格式
Dim s2() As String '原图片的地址,用"\"分割为数组
Sub upimg()
If file.PostedFile.ContentLength > 0 Then
Dim fileSize As Integer = file.PostedFile.ContentLength
If fileSize < 200000 Then
s2 = file.PostedFile.FileName.Split("\")
s1 = Right(s2(s2.Length - 1), 4)
If s1 = ".jpg" Or s1 = ".JPG" Or s1 = ".GIF" Or s1 = ".gif" Or s1 = ".bmp" Or s1 = ".BMP" Then
Dim s3 As String '上传后图片的名称,时间+毫秒为名字
s3 = Format(Now(), "yyyyMMddHHmmss") & Trim(Str(DateTime.Now.Millisecond)) & s1
file.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(".") & "\images\" & s3) '保存
Maximg = "~\images\" & s3
minup()
Else
Msglab.Text = "图片的格式不符合要求"
End If
Else
Msglab.Text = "图片大小超过了200K"
End If
Else
Msglab.Text = "请选择图片"
End If
End Sub
'缩略图上传
Sub minup()
Dim simg As System.Drawing.Image '声明一个image对象
Dim H As Integer '图像高度
Dim W As Integer '图像宽度
simg = System.Drawing.Image.FromFile(file.PostedFile.FileName) '实例化一个image对象
H = simg.Height
W = simg.Width
If H > W Then
y = y
x = x * (W / H)
Else
x = x
y = y * (H / W)
End If
Dim callback As GetThumbnailImageAbort
simg = simg.GetThumbnailImage(x, y, callback, IntPtr.Zero) '生成缩略图
Dim s4 As String
s4 = Format(Now(), "yyyyMMddHHmmss") & Trim(Str(DateTime.Now.Millisecond)) & "s" & s1
simg.Save(HttpContext.Current.Server.MapPath(".") & "\images\" & s4) '保存在指定文件夹
Minimg = "~\images\" & s4
End Sub
End Class