沿 GraphicsPath 创建新图片

问题描述

有什么办法可以把一个GraphicsPath和封闭的图形复制到新的图片中吗?
我有矩形,GraphicsPath 的点可用。路径肯定在矩形内。
我已经用谷歌搜索过,但结果很差。到目前为止,我只能将某个区域(矩形)复制到新图片中,请参阅源代码

Using Extracted As Bitmap = New Bitmap(rect.Width,rect.Height,Imaging.PixelFormat.Format32bppArgb)
    Using Grp As Graphics = Graphics.FromImage(Extracted)
        Grp.DrawImage(Picture1,rect,GraphicsUnit.Pixel)
    End Using
    If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
        Extracted.Save("C:\Users\xy\Desktop\1.png",Imaging.ImageFormat.Png)
    End If
End Using

解决方法

我发现了一些东西here

这是翻译成 VB.Net 并带有 Option Strict On 和 Option Infer Off 的解决方案。

Using bmpSource As Bitmap = New Bitmap(Form1.Pfad_Bild)
                    Dim rectCutout As RectangleF = gp.GetBounds()
                    Using m As Matrix = New Matrix()
                        m.Translate(-rectCutout.Left,-rectCutout.Top)
                        gp.Transform(m)
                    End Using
                    Using bmpCutout As Bitmap = New Bitmap(CInt(rectCutout.Width),CInt(rectCutout.Height))
                        Using graphicsCutout As Graphics = Graphics.FromImage(bmpCutout)
                            graphicsCutout.Clip = New Region(gp)
                            graphicsCutout.DrawImage(bmpSource,CInt(-rectCutout.Left),CInt(-rectCutout.Top))
                            If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
                                bmpCutout.Save("C:\Users\xy\Desktop\1.png",Imaging.ImageFormat.Png)
                            End If
                        End Using
                    End Using
                End Using