Full class for making the clearest thumbnail

xiaoxiao2021-03-06  62

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.image.imageformat

Private Minheight, MinWidth As Decimal

Private myfile as it.file

Public Sub New (Byval Picfile As System.Web.ui.htmlControls.htmlInputFile, Byval Victype As Pictype)

vpicfile = picfile

Vupfilesize = httpContext.current.Application ("UpFileSize")

SELECT CASE EPPICTYPE

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.toTString () & "." & getrightBychar (vpicfile.postedfile.filename, ".")

Case Pictype.pic

VuppicPath = "UPLOAD / IMAGES / PIC"

vsmallpicsize = 550

vnewpicname = system.guid.newguid.toTString () & "." & getrightBychar (vpicfile.postedfile.filename, ".")

End SELECT

End Sub

Public function GetsavedFileName () AS String

'Check picture type =============================================== ===================

IF vpicfile.postedfile.filename = "" "

Throw new NotSupportedException ("The file is empty, please select the image file uploaded!") End IF

If Left (VPICFILE.POSTEDFILE.CONTENTTYPE, 5) <> "image"

Throw new NotSupportedException ("File format is not legal, select a valid image file!" & Vpicfile.postedfile.contentType)

END IF

IF vpicfile.postedfile.contentLength> Vupfilesize Then

DIM MAXNUMBER As Decimal = VupfileSize / 1024/1024

Throw New NotSupportedException ("Uploaded Picture 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 thein

VTMPPICNAME = System.guid.newguid.toString () & ".png"

vpicmax = picmax

Picmax.save (httpContext.current.server.mappath (vuppicpath & "/ max /") & vtmpicname, picformat.png)

vpicmax.dispose ()

Picmax = system.drawing.image.fromfile (httpContext.current.server.mappath (vuppicpath & "/ max /") & vtmppicname)

END IF

END IF

'Save small file ============================================= ===================

GetMinPic (Picmax) .save (httpcontext.current.server.mappath (vuppicpath & "/ min /") & vnewpicname, picformat.jpeg)

Picmax.dispose ()

'Delete temporary PNG file ================================================ ====================

IF vtMPPICNAME <> "" "THENMEFILE.DELETE (httpContext.current.server.mappath (vuppicpath &" / max / ") & vtmppicname)

Return vnewpicname

END FUNCTION

Private function getMinpic (Byval MaxPic As System.drawing.Image) As system.drawing.imageif maxpic.height> vsmallpicsize or maxpic.width> vsmallpicsize thein

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 (MinHeight), Nothing, New System.intptr ())

Else

Return MaxPic

END IF

END FUNCTION

ENUM PICTYPE

Face = 1

Photo = 2

PIC = 3

END ENUM

Private function getrightbychar (byval strign as string) AS STRING

DIM mystr () as string = split (strign, charvalue)

Return mystr (MyStr.length - 1)

END FUNCTION

END CLASS

转载请注明原文地址:https://www.9cbs.com/read-112102.html

New Post(0)