首页 > 编程 > .NET > 正文

制作最清晰缩略图的完整类(VB.NET版)

2024-07-10 13:00:44
字体:
来源:转载
供稿:网友
先收集一些相关资源
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

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

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

picmax.dispose()



'删除临时png文件=================================================================

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

转自:http://guoblog.com/blogview.asp?logid=259

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表