vb.netgraphicspath

Create a new picture along the GraphicsPath


Is there some way to copy a GraphicsPath and the enclosed figure into a new picture?
I have the rectangle, the points of the GraphicsPath available. The path is definitely in the rectangle.
I already googled but the result is poor. So far, I can only copy a certain area (rectangle) into a new picture, see source code.

Using Extracted As Bitmap = New Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
    Using Grp As Graphics = Graphics.FromImage(Extracted)
        Grp.DrawImage(Picture1, 0, 0, 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

Solution

  • I have found something here:

    This is the solution translated into VB.Net and with Option Strict On and 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