可以把所有画的线都保存在一个列表中,画的时候全部画出即可。如下:
Public Class Form1
Class Line '直线类
Public Point1, Point2 As Point '成员,直线的两个端点
Sub New(p1 As Point, p2 As Point) '构造方法
Point1 = p1
Point2 = p2
End Sub
Public Sub Draw(g As Graphics) '绘制方法
g.DrawLine(Pens.Black, Point1, Point2)
End Sub
End Class
Private Lines As New List(Of Line) '列表用于保存所有画下的直线
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackColor = Color.White
DoubleBuffered = True '开启双缓冲可有效避免闪烁
End Sub
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
Lines.Add(New Line(e.Location, e.Location)) '在直线列表中添加直线
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button <> Windows.Forms.MouseButtons.Left Then Return '左键未按下
'鼠标拖动时改变列表最后一条直线(也即当前直线的第二个端点)
Lines(Lines.Count - 1).Point2 = e.Location
Refresh() '刷新窗体
End Sub
'在Form的Paint事件中绘制所有直线,每次Form1重绘时都会触发Paint事件
'PS: 也可以通过重写OnPaint方法来达到类似的效果
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias '开启抗锯齿
For Each l In Lines '遍历所有直线
l.Draw(e.Graphics) '调用绘制方法,传入的参数可以理解为画布
Next
End Sub
End Class
运行效果:
首先,你必须建立一个位图
图像控制图像性能,然后以图片相关联的图像
上画线
图像保存到一个文件中
< / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
暗淡MYBITMAP作为图象
MYBITMAP =新的Bitmap(300,150)
PictureBox0.Image:= MYBITMAP
点心作为图形的
走势图= Graphics.FromImage(PictureBox0.image)的
走势图走势图。 drawLine的(Pens.Blue,0,0,111,111)
picturebox0.image.save(“C:\ aaa.jpg”)
/ / / / / / / / / / / / / / / / / /我一直在使用这个方法/ / / / / / / / / / / / / / / / / / /
保存为文件,绝对是你画的线......
先要建立bitmap,使它与PictureBox1关联
Dim mybitmap As Bitmap
Dim Graph As Graphics
下面的可以放在new里面
mybitmap = New Bitmap(300, 150)
PictureBox1.Image = mybitmap
Graph = Graphics.FromImage(PictureBox1.Image)
PictureBox1_MouseUp里面改成下面的
Graph.DrawRectangle(Pens.Red, x1, y1, x2, y2)
PictureBox1.Image.Save("D:/1.jpg")
qq = False
PictureBox1.Refresh()
刷新了,去掉这一句看看。
不说假话,这个代码我真的不懂。