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

时间:2014-10-09 12:56:12

标签: .net vb.net visual-studio-2013

可以使用PictureBox上的图片使用Graphics.DrawLine保存图形创建吗?

它看起来像这样:

http://pl.tinypic.com/r/140k6xf/8

我的绘图功能:

 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

我正在另一个程序中加载图片,然后通过DrawLine添加行,我需要保存它。我尝试使用互联网上的一些不同的例子,但在我的情况下它不起作用。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您应该能够使用Graphics.DrawImage创建新的位图,在其上绘制图形内容,然后使用Bitmap.Save进行保存。