VB.NET实现缩略图案例解析

开发 后端
文章主要VB.NET实现缩略图代码的演示,详细代码如Public Class ClassUpPic Private vPicFile As System.Web.UI.HtmlControls.HtmlInputFile ...。

运用了VB.NET开发很长时间了,总结了一点经验,我发现有些人为缩略图问题烦恼,在这里呢我来用VB.NET实现缩略图代码解决这个问题。

VB.NET实现缩略图代码:

  1. Public Class ClassUpPic  
  2. Private vPicFile As System.Web.UI.HtmlControls.HtmlInputFile  
  3. Private vSmallPicSize, vUpFileSize As Integer  
  4. Private vUpPicPath, vNewPicName, vTmpPicName As String  
  5. Private PicMin, PicMax, vPicMax As System.Drawing.Image  
  6. Private PicFormat As System.Drawing.Imaging.ImageFormat  
  7. Private MinHeight, MinWidth As Decimal  
  8. Private Myfile As IO.File  
  9. Public Sub New(ByVal PicFile As System.Web.UI.HtmlControls.HtmlInputFile, ByVal UpPicType As PicType)  
  10. vPicFile = PicFile  
  11. vUpFileSize = HttpContext.Current.Application("UpFileSize")  
  12. Select Case UpPicType  
  13. Case PicType.Face  
  14. vUpPicPath = "upload/images/Face" 
  15. vSmallPicSize = 150 
  16. vNewPicName = HttpContext.Current.Session("MemberID") & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")  
  17. Case PicType.Photo  
  18. vUpPicPath = "upload/images/Photo" 
  19. vSmallPicSize = 150 
  20. vNewPicName = System.Guid.NewGuid.ToString() & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")  
  21. Case PicType.Pic  
  22. vUpPicPath = "upload/images/Pic" 
  23. vSmallPicSize = 550 
  24. vNewPicName = System.Guid.NewGuid.ToString() & "." & GetRightByChar(vPicFile.PostedFile.FileName, ".")  
  25. End Select  
  26. End Sub  
  27. Public Function GetSavedFileName() As String  
  28. '检验图片类型=================================================================  
  29. If vPicFile.PostedFile.FileName = "" Then  
  30. Throw New NotSupportedException("文件为空,请您选择上传的图片文件!")  
  31. End If  
  32. If Left(vPicFile.PostedFile.ContentType, 5) <> "image" Then  
  33. Throw New NotSupportedException("文件格式不合法,请选取有效的图片文件!" & vPicFile.PostedFile.ContentType)  
  34. End If  
  35. If vPicFile.PostedFile.ContentLength > vUpFileSize Then  
  36. Dim MaxNumber As Decimal = vUpFileSize / 1024 / 1024  
  37. Throw New NotSupportedException("上传的图片文件太大,***支持" & Format(MaxNumber, "##,##0") & "M!")  
  38. End If  
  39. '检验数量限制=================================================================  
  40. '保存大文件=================================================================  
  41. vPicFile.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vNewPicName)  
  42. vPicFile.Dispose()  
  43. '缩略图片文件=================================================================  
  44. PicMax = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vNewPicName)  
  45. If Not (PicMax.RawFormat Is PicFormat.Gif Or PicMax.RawFormat Is PicFormat.Png) Then  
  46. If PicMax.Height > vSmallPicSize Or PicMax.Width > vSmallPicSize Then  
  47. vTmpPicName = System.Guid.NewGuid.ToString() & ".png"  
  48. vPicMax = PicMax  
  49. PicMax.Save(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vTmpPicName, PicFormat.Png)  
  50. vPicMax.Dispose()  
  51. PicMax = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vTmpPicName)  
  52. End If  
  53. End If  
  54. '保存小文件=================================================================  
  55. GetMinPic(PicMax).Save(HttpContext.Current.Server.MapPath(vUpPicPath & "/min/") & vNewPicName, PicFormat.Jpeg)  
  56. PicMax.Dispose()  
  57. '删除临时png文件=================================================================  
  58. If vTmpPicName <> "" Then Myfile.Delete(HttpContext.Current.Server.MapPath(vUpPicPath & "/max/") & vTmpPicName)  
  59. Return vNewPicName  
  60. End Function  
  61. Private Function GetMinPic(ByVal MaxPic As System.Drawing.Image) As System.Drawing.Image  
  62. If MaxPic.Height > vSmallPicSize Or MaxPic.Width > vSmallPicSize Then  
  63. If MaxPic.Height > MaxPic.Width Then  
  64. MinWidth = MaxPic.Width / (MaxPic.Height / vSmallPicSize)  
  65. MinHeight = vSmallPicSize 
  66. Else  
  67. MinWidth = vSmallPicSize 
  68. MinHeight = MaxPic.Height / (MaxPic.Width / vSmallPicSize)  
  69. End If  
  70. Return MaxPic.GetThumbnailImage(CInt(MinWidth), CInt(MinHeight), Nothing, New System.IntPtr())  
  71. Else  
  72. Return MaxPic  
  73. End If  
  74. End Function  
  75. Enum PicType  
  76. Face = 1 
  77. Photo = 2 
  78. Pic = 3 
  79. End Enum  
  80. Private Function GetRightByChar(ByVal StrValue As String, ByVal CharValue As String) As String  
  81. Dim MyStr() As String = Split(StrValue, CharValue)  
  82. Return MyStr(MyStr.Length - 1)  
  83. End Function  
  84. End Class 

以上就是VB.NET实现缩略图的代码,试试吧!

【编辑推荐】

  1. 剖析VB.NET平台调用是如何执行操作
  2. 分享个人总结VB.NET多线程
  3. 详细说明VB.NET变量中四点
  4. 三类十二种VB.NET数据类型全面介绍
  5. VB.NET初步知识,初学者必看
责任编辑:田树 来源: 乐博网
相关推荐

2009-08-12 16:33:37

.NET生成缩略图

2009-08-28 15:19:17

C#实现缩略图

2013-08-12 15:26:49

测试

2009-10-26 17:10:53

VB.NET word

2009-11-03 10:18:46

VB.NET继承

2009-10-29 14:02:24

VB和VB.NET比较

2013-12-02 15:07:57

jQuery插件

2010-01-21 17:48:25

VB.NET Sing

2010-01-15 16:12:40

VB.NET调用DLL

2012-07-18 20:59:40

jQuery

2009-11-02 16:55:50

VB.NET Smar

2019-02-15 14:00:57

Linux命令缩略图

2009-12-07 11:21:59

PHP生成缩略图

2009-10-09 15:59:41

VB.NET对象

2010-01-15 15:03:23

VB.NET对象变量声

2009-11-03 13:16:58

VB.NET读取文件

2009-08-28 10:22:13

Windows 7系统故障应对缩略图无法显示

2012-01-10 14:59:42

jQuery

2010-01-22 15:47:37

VB.NET初始化网格

2010-01-12 11:17:21

VB.NET文字特效
点赞
收藏

51CTO技术栈公众号