To gather some resources 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 vnewpicna me = 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, ".") EndsavedFileName () AS String 'Check the picture type ============= ======================
============================== if vpicfile.postedFile.FileName = "" "" The Document is empty, please You choose the image file you selected! ") End if if ipostedfile.contentType, 5) <>" Image "The Throw new NotSupportedException (" File format is not legal, please select a valid picture file! "& Vpicfile.postedFile .ContentType) End If If vPicFile.PostedFile.ContentLength> vUpFileSize Then Dim MaxNumber As Decimal = vUpFileSize / 1024/1024 Throw New NotSupportedException ( "upload image file is too large, maximum support" & Format (MaxNumber, "##, ## 0 ") &" m! ") End if 'Test quantity limit ==================================== ============================== 'Save big file ================= ========================================================== vpicfile .Postedfile.saveas (httpContext.current.server.mappath (vuppicpath & "/ max /") & vnewpicname) VPICFILE.DISPOSE () 'thumbnail image file ================
============================================================================================================================================================================================================= 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 'save small files ============================================================================================================================================================================================================= ================ GetMinPic (PICMAX) .save (httpContext.current.server.mappath (VuppicPath & "/ min /") & vnewpicname, picformat.jpeg) Picmax.dispose () 'Delete temporary PNG file ============= ==========================
==================================================================================================================== # / ") & vTmpPicName) Return vNewPicName End Function Private Function GetMinPic (ByVal MaxPic As System.Drawing.Image) As System.Drawing.Image If MaxPic.Height> vSmallPicSize Or MaxPic.Width> vSmallPicSize Then If MaxPic.Height> MaxPic.Width Then MinWidth = MaxPic.Width / (MaxPic.Height / vSmallPicSize) MinHeight = vSmallPicSize Else MinWidth = vSmallPicSize MinHeight = MaxPic.Height / (MaxPic.Width / vSmallPicSize) End If Return MaxPic.GetThumbnailImage (CInt (MinWidth), CInt (MinHeight) , New system.intptr ()) ELSE RETURN MAXPIC END IF End Function Enum Pictype Face = 1 Photo = 2 Pic = 3 End Enum Private Function GetRightBychar (Byval Strval As String, ByVal Charvalue AS String) AS String Dim MyStr () AS String = Split (Strignue, Charvalue) Return MyStr (MyStr.Length - 1) End Functionend Class Transfer from: http://guoblog.com/blogview.asp? LogID = 259