0

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
Jimi
  • 29,621
  • 8
  • 43
  • 61
Daniel
  • 374
  • 1
  • 12
  • Well, a GraphicsPath is not related to a specific Device Contex, until you draw that path. You can use your Graphics object to draw the same GraphicsPath to any DC. Of course the DC where the GraphicsPath is drawn dictates *the rules*; e.g., it applies its own transformations and the Points are relative to that specific Context. Two Bitmaps with the same Size and Dpi descriptor, will draw the same path in the same position. – Jimi Jan 17 '21 at 21:18
  • Btw, you're not using a GraphicsPath anywhere here, so it's difficult to see where the problem is, might be or what you actually want to do. – Jimi Jan 17 '21 at 21:51
  • Hi Jimi, Once again in more detail: I wrote a program where I read in the original image, use Laplace to make an edge image from it, recognize the edges in a drawn rectangle, and finally where the GraphicsPath is drawn. Now I would like to save a new picture, with the rectangular dimensions (i.e. cut out), but everything transparent except for the inside of the path. I don't know how to get the graph onto the new picture. Can you help me please? – Daniel Jan 18 '21 at 11:11
  • Or how do I make a method that finds out if the current x, y is inside or outside the GraphicsPath? – Daniel Jan 18 '21 at 11:50
  • Draw the original Image to a new Bitmap, derived the Graphics object from this Bitmap, call [e.Graphics.SetClip()](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.setclip) passing the GraphicsPath you have. Draw the original Bitmap into the new one. The Graphics will clip the original Image using the GraphicsPath boundaries. Note that clipping prevents anti-alias. If you don't like the result, you can use a TexttureBrush instead, as shown here: [How to crop an elliptical region of an Image with smooth borders](https://stackoverflow.com/a/61554272/7444103) – Jimi Jan 18 '21 at 16:58
  • As mentioned, the two Bitmaps - Source and Destination - must have the same size and DPI descriptor. – Jimi Jan 18 '21 at 17:07
  • Hi Jimi, basically I understood what you meant, only that I was not familiar with the SetClip. I used other search terms again and found something. [link](https://stackoverflow.com/questions/33820712/clip-an-image-in-a-specific-shape-net) – Daniel Jan 20 '21 at 12:11

1 Answers1

0

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
Daniel
  • 374
  • 1
  • 12