[VB] 图片等比例缩小方法
发布时间:2020-12-17 07:29:22 所属栏目:百科 来源:网络整理
导读:摘要:[VB] 图片等比例缩小方法 将图片等比例缩小的方法 ‘设定图片长宽 Public Sub setImageWH() Try ‘图片位置 Dim FilePath As String = "~/Images/Img.png" ‘取得图片真实路径 Dim realPath As String = HttpContext.Current.Request.MapPath(FilePath)
摘要:[VB] 图片等比例缩小方法 将图片等比例缩小的方法 ‘设定图片长宽 Public Sub setImageWH() Try ‘图片位置 Dim FilePath As String = "~/Images/Img.png" ‘取得图片真实路径 Dim realPath As String = HttpContext.Current.Request.MapPath(FilePath) ‘ Dim ImgDw As System.Drawing.Image = System.Drawing.Image.FromFile(realPath) ‘取得缩图后的长宽 Dim imgSize() As Integer = getThumbnailImageScale(120,100,ImgDw.Width,ImgDw.Height) ImgDw.Dispose() ‘设定图片长宽 imgTest.Width = imgSize(0) imgTest.Height = imgSize(1) Catch ex As Exception End Try End Sub ‘保存时使用 Private Sub imgresize(ByVal width As Integer,ByVal height As Integer,ByVal saveurl As String,ByVal filebytes As Byte(),ByVal filelength As Integer) Dim ms As MemoryStream = New MemoryStream ms.Write(filebytes,filelength) ms.Flush() Dim img1 As System.Drawing.Image = System.Drawing.Image.FromStream(ms) If img1.Height > height Or img1.Width > width Then ‘是否有超高or宽 Dim newimg As System.Drawing.Image Dim thumbnailScale As Integer() = getThumbnailImageScale(width,height,img1.Width,img1.Height) ‘算出原图长宽比 newimg = img1.GetThumbnailImage(thumbnailScale(0),thumbnailScale(1),Nothing,IntPtr.Zero) ‘释放资源 newimg.Save(saveurl) newimg.Dispose() img1.Dispose() ms.Close() End If End Sub ‘带入限制范围长宽跟原图片长宽 Private Function getThumbnailImageScale(ByVal maxWidth As Integer,ByVal maxHeight As Integer,ByVal oldWidth As Integer,ByVal oldHeight As Integer) As Integer() Dim result() As Integer = New Integer() {0,0} Dim widthDividend As Single,heightDividend As Single,commonDividend As Single widthDividend = oldWidth / maxWidth heightDividend = oldHeight / maxHeight If (heightDividend > widthDividend) Then commonDividend = heightDividend Else commonDividend = widthDividend End If result(0) = CType((oldWidth / commonDividend),Integer) result(1) = CType((oldHeight / commonDividend),Integer) Return result End Function 以上文章叙述如有错误及观念不正确,请不吝啬指教 原文:大专栏 ?[VB] 图片等比例缩小方法 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 正则表达式 验证YYYY-MM-DD HH:mm:ss,包含闰年验
- react-native – 在AsyncStorage中使用setItem的
- [Swift]LeetCode240. 搜索二维矩阵 II | Search
- ruby-on-rails – 根据记录和订单条件,在之后或之
- c – Boost Graph Library astar和导航网格
- ruby – 从OAuth安全地存储token / secret / etc
- c# – LINQ to Entities对带有嵌套对象的Union的
- c# – 如何确定var的大小?
- 数组 – 在转换为数组的文本列上创建postgresql索
- SQLITE3 --详解
热点阅读