以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  正方形图片伸缩成梯形图片  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=192058)

--  作者:genggeng
--  发布时间: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
图片点击可在新窗口打开查看

--  作者:有点蓝
--  发布时间:2024/5/27 9:53:00
--  
微软文档的里这个方法只有2个和4个参数,没有4个参数的

https://learn.microsoft.com/zh-cn/dotnet/api/system.drawing.drawing2d.matrix.shear?view=netframework-4.0&redirectedfrom=MSDN#System_Drawing_Drawing2D_Matrix_Shear_System_Single_System_Single_System_Drawing_Drawing2D_MatrixOrder_