日期:2012-12-15  浏览次数:20464 次

先收集一些相关资源
Public Class ClassUpPic

Private vPicFile As System.Web.UI.HtmlControls.HtmlInputFile

Private vSmallPicSize, vUpFileSize As Integer

Private vUpPicPath, vNewPicName, vTmpPicName As String

Private PicMin, PicMax, vPicMax As System.Drawing.Image

Private PicFormat As System.Drawing.Imaging.ImageFormat

Private MinHeight, MinWidth As Decimal

Private Myfile As IO.File



Public Sub New(ByVal PicFile As System.Web.UI.HtmlControls.HtmlInputFile, ByVal UpPicType As PicType)

vPicFile = PicFile

vUpFileSize = HttpContext.Current.Application("UpFileSize")

Select Case UpPicType

Case PicType.Face

vUpPicPath = "upload/images/Face"

vSmallPicSize = 150

vNewPicName = HttpContext.Current.Session("MemberID") & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")

Case PicType.Photo

vUpPicPath = "upload/images/Photo"

vSmallPicSize = 150

vNewPicName = System.Guid.NewGuid.ToString() & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")

Case PicType.Pic

vUpPicPath = "upload/images/Pic"

vSmallPicSize = 550

vNewPicName = System.Guid.NewGuid.ToString() & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")

End Select

End Sub



Public Function GetSavedFileName() As String

'检验图片类型=================================================================

If vPicFile.PostedFile.FileName = "" Then

Throw New NotSupportedException("文件为空,请您选择上传的图片文件!")

End If

If Left(vPicFile.PostedFile.ContentType, 5) <> "image" Then

Throw New NotSupportedException("文件格式不合法,请选取有效的图片文件!" & vPicFile.PostedFile.ContentType)

End If

If vPicFile.PostedFile.ContentLength > vUpFileSize Then

Dim MaxNumber As Decimal = vUpFileSize / 1024 / 1024

Throw New NotSupportedException("上传的图片文件太大,最大支持" & Format(MaxNumber, "##,##0") & "M!")

End If



'检验数量限制=================================================================



'保存大文件=================================================================

vPicFile.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vNewPicName)

vPicFile.Dispose()



'缩略图片文件=================================================================

PicMax = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vNewPicName)

If Not (PicMax.RawFormat Is PicFormat.Gif Or PicMax.RawFormat Is PicFormat.Png) Then

If PicMax.Height > vSmallPicSize Or PicMax.Width > vSmallPicSize Then

vTmpPicName = System.Guid.NewGuid.ToString() & ".png"

vPicMax = PicMax

PicMax.Save(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vTmpPicName, PicFormat.Png)

vPicMax.Dispose()

PicMax = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vTmpPicName)

End If

End If

'保存小文件==========================================