加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[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] 图片等比例缩小方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读