【问题标题】:Create a new picture along the GraphicsPath沿 GraphicsPath 创建新图片
【发布时间】:2021-01-17 21:12:07
【问题描述】:

有没有办法将 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, 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

【问题讨论】:

  • 好吧,GraphicsPath 与特定的设备上下文无关,直到您绘制该路径。您可以使用您的 Graphics 对象将相同的 GraphicsPath 绘制到任何 DC。当然,绘制 GraphicsPath 的 DC 规定了规则;例如,它应用自己的转换,并且点是相对于该特定上下文的。两个具有相同 Size 和 Dpi 描述符的 Bitmap,将在相同位置绘制相同的路径。
  • 顺便说一句,您在这里的任何地方都没有使用 GraphicsPath,因此很难看出问题出在哪里、可能是什么或您真正想要做什么。
  • 嗨 Jimi,再次详细说明:我编写了一个程序,在其中读取原始图像,使用 Laplace 从中制作边缘图像,识别绘制矩形中的边缘,最后在哪里GraphicsPath 被绘制。现在我想保存一张新图片,具有矩形尺寸(即切出),但除了路径内部之外的所有内容都是透明的。我不知道如何将图表放到新图片上。你能帮帮我吗?
  • 或者我如何创建一个方法来确定当前 x, y 是在 GraphicsPath 内部还是外部?
  • 将原始图像绘制到新的位图,从该位图派生图形对象,调用e.Graphics.SetClip()传递您拥有的GraphicsPath。将原始位图绘制到新的位图中。 Graphics 将使用 GraphicsPath 边界剪辑原始图像。请注意,剪裁可防止抗锯齿。如果您不喜欢结果,可以使用 TexttureBrush 代替,如下所示:How to crop an elliptical region of an Image with smooth borders

标签: vb.net graphicspath


【解决方案1】:

我发现了一些东西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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多