Foxtable(狐表)用户栏目专家坐堂 → 正方形图片伸缩成梯形图片


  共有246人关注过本帖树形打印复制链接

主题:正方形图片伸缩成梯形图片

帅哥哟,离线,有人找我吗?
genggeng
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:婴狐 帖子:17 积分:249 威望:0 精华:0 注册:2022/8/18 11:23:00
正方形图片伸缩成梯形图片  发帖心情 Post By:2024/5/27 9:02:00 [只看该作者]

代码如下,在执行
' 应用梯形变换
        matrix.Shear(topCenter, bottomCenter, heightFactor, 0)
时报错。请问该如何解决?


Dim originalImage As Image = Image.FromFile(txt3 & "\" & t(zdw) & ".png")
    
    ' 创建一个新的Bitmap对象,用于存储转换后的梯形图片
    Dim width As Integer = originalImage.Width
    Dim height As Integer = originalImage.Height
    Dim newImage As New Bitmap(width, height)
    
    ' 使用Graphics对象来绘制图像
    Using g As Graphics = Graphics.FromImage(newImage)
        ' 创建一个Matrix对象来定义变换
        Dim matrix As New Matrix()
        
        ' 定义梯形的变换参数
            ' 这里假设我们想要一个顶部宽度比底部宽度小的梯形
        ' 需要根据实际需求调整参数
        Dim topWidth As Single = width * 0.8F
        Dim bottomWidth As Single = width
        Dim heightFactor As Single = 1F
        
        ' 计算梯形的顶部和底部中心点
        Dim topCenter As Single = topWidth / 2F
        Dim bottomCenter As Single = bottomWidth / 2F
        
        ' 应用梯形变换
        matrix.Shear(topCenter, bottomCenter, heightFactor, 0)
        
        ' 创建一个GraphicsPath对象来定义梯形的路径
        Dim path As New GraphicsPath()
        path.AddLine(topCenter, 0, topCenter, heightFactor * height)
        path.AddLine(topCenter, heightFactor * height, bottomCenter, height)
        path.AddLine(bottomCenter, height, bottomCenter + (bottomWidth - topWidth) / 2, heightFactor * height)
        path.AddLine(bottomCenter + (bottomWidth - topWidth) / 2, heightFactor * height, bottomCenter + (bottomWidth - topWidth), 0)
        path.CloseAllFigures()
        
        ' 将路径应用于Graphics对象
        g.SmoothingMode = SmoothingMode.AntiAlias
        g.InterpolationMode = InterpolationMode.HighQualityBicubic
        g.PixelOffsetMode = PixelOffsetMode.HighQuality
        g.DrawImage(originalImage, path)
        
        ' 显示转换后的梯形图片
        p3.Image = newImage
    End Using
    newImage.Save(txt3 & "\" & t(zdw) & "-tx.png")
图片点击可在新窗口打开查看此主题相关图片如下:1.png
图片点击可在新窗口打开查看

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107604 积分:547323 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/5/27 9:53:00 [只看该作者]


 回到顶部