.net 如何在PictureBox上保存Graphics.DrawLine创建的图形

.net 如何在PictureBox上保存Graphics.DrawLine创建的图形,.net,vb.net,visual-studio-2013,.net,Vb.net,Visual Studio 2013,是否可以使用此PictureBox中的图片保存PictureBox上的Graphics.DrawLine创建的图形 它是这样看的: 我的绘图功能: Public Sub PictureBox2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If PictureBox1.Image Is Nothing Then

是否可以使用此PictureBox中的图片保存PictureBox上的Graphics.DrawLine创建的图形

它是这样看的:

我的绘图功能:

 Public Sub PictureBox2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If PictureBox1.Image Is Nothing Then
        MsgBox("Upload Image first", MsgBoxStyle.Critical)
    Else


        Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)

        Dim blackpen As New Drawing.Pen(ColorDialog1.Color, 0.5)
        Dim currPoint As Point

        Dim drawFont As New Font("Arial", 8)
        Dim drawBrush As New SolidBrush(ColorDialog1.Color)
        Dim drawFormat As New StringFormat
        drawFormat.Alignment = StringAlignment.Center

        g = PictureBox1.CreateGraphics()

        If previousPoint.IsEmpty = True Then
            previousPoint = New Point(e.X - 231, e.Y - 52)
            MPx1 = e.X
            MPy1 = e.Y
        Else
            currPoint = New Point(e.X - 231, e.Y - 52)
            MPx2 = e.X
            MPy2 = e.Y
            g.DrawLine(blackpen, previousPoint, currPoint)


            res = ((((MPx1 - MPx2) ^ 2) + ((MPy1 - MPy2) ^ 2)) ^ 0.5) / Val(Label11.Text)
            g.DrawString(res, drawFont, drawBrush, currPoint, drawFormat)
            previousPoint = New Point(0, 0)

        End If
        blackpen.Dispose()
        drawFont.Dispose()
        drawBrush.Dispose()
        drawFormat.Dispose()
        g.Dispose()
    End If

我在另一个程序中加载图片,然后通过抽绳添加线条,我需要保存它。我试着使用互联网上的一些例子,但在我的例子中,它不起作用。有人能帮我吗?

您应该能够创建一个新位图,使用在上面绘制图形内容,然后使用保存。

使用graphics.FromImage()而不是PictureBox1.CreateGraphics()。您将修改当前显示在图片框中的图像。你从一开始就知道了,但现在你正在覆盖g变量。在此之后,您可能还需要强制进行PictureBox刷新。