Make the clearest thumbnail of the complete class (VB. NET edition) __.net

Source: Internet
Author: User
Tags httpcontext
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

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

If vPicFile.PostedFile.FileName = "" Then

Throw New NotSupportedException ("File is blank, please select upload picture file.") ")

End If

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

Throw New notsupportedexception ("file format is not valid, 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 a picture file too large, maximum support" & Format (MaxNumber, "##,# #0") & "M. ")

End If



' Inspection quantity limit =================================================================



' Save large 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 are 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 file =================================================================

Getminpic (Picmax). Save (HttpContext.Current.Server.MapPath (Vuppicpath & "/min/") & Vnewpicname, Picformat.jpeg)

Picmax.dispose ()



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

If vtmppicname <> "" Then Myfile.delete (HttpContext.Current.Server.MapPath (Vuppicpath & "/max/") & 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), 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 strvalue As String, ByVal Charvalue as String) as String

Dim mystr () as String = Split (strvalue, Charvalue)

Return MyStr (mystr.length-1)

End Function

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.