vb.net没有自动重画功能,要在Paint事件中写代码对图形重画。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、网页空间、营销软件、网站建设、龙圩网站维护、网站推广。
另外一种情况,如果在Image属性设置了一幅图像,图像能够保持完整性的。所以你可以把图形绘在位图上,把位图绑定到Image属性上。
先绑定一幅位图:
Dim bm as New BitMap(800,600)
PictureBox1.Image=bm
作图时不是对图片框,而是在位图上作图。
dim gr As Grapthics=Graphics.FromImage(bm) '建立位图的绘图设备
接下来就可用gr 的绘图方法作图
作完图,PictureBox1.Refresh 刷新一下。
不引用的话,VB做不到。这事情要看VB的版本。如果是6.0的话,要去网上下载GDIPLUS的库文件或者自己声明GDI+的API。如果是VB.NET的话,VB自带GDI+,但是也可以下载GDIPLUS库来用。如果不知道去哪里下载,我下载有,你可以问我要。我使用VB6.0。下载gdiplus以后,在VB里面引用这个库,注意要选择“所有文件”才能看到这个库。gdi+里面的path功能可以实现样条:Private
TOKEN
As
Long'GDI+对象
Private
Graphics
As
Long'画板
Private
Sub
InitGDIPlus()
'初始化GDI+
Dim
uInput
As
GdiplusStartupInput
uInput.GdiplusVersion
=
1
If
GdiplusStartup(TOKEN,
uInput)
Ok
Then
'初始化错误
MsgBox
"GDI+
初始化错误。程序即将关闭。",
vbCritical,
"InitError"
End
End
If
GdipCreateFromHDC
Me.hDC,
Graphics'创建画板
GdipSetSmoothingMode
Graphics,
SmoothingModeAntiAlias'设置为反锯齿
End
SubPrivate
Sub
TerminateGDIPlus()
GdipDeleteGraphics
Graphics
'释放graphics占用的内存
GdiplusShutdown
TOKEN
'关闭GDI+
End
SubPrivate
Sub
Form_Load()
InitGDIPlus
'初始化End
SubPrivate
Sub
Command1_Click()
Dim
path
As
Long
Dim
m(3)
As
POINTF
'以下是坐标,你可以自由改变
m(0).x
=
m(0).y
=
m(1).x
=
10
m(1).y
=
100
m(2).x
=
20
m(2).y
=
3
m(3).x
=
500
m(3).y
=
100
Dim
pen
As
Long
GdipCreatePen1
HFF000000,
2,
UnitPixel,
pen
'创建画笔,用来画出样条
GdipCreatePath
FillModeAlternate,
path
'创建path
GdipAddPathBeziers
path,
m(0),
4
'创建样条'Count是说坐标的个数,points只能传递数组的第一个元素,不能传递数组。
GdipDrawPath
Graphics,
pen,
path
'画出样条
GdipDeletePen
pen
'删除画笔
GdipDeletePath
path
'删除样条End
SubPrivate
Sub
Form_Unload(Cancel
As
Integer)
TerminateGDIPlus
'删除GDI+
End
Sub
Imports System.Drawing.Imaging
Public Class Form1
Dim imageName As String = "C:\Documents and Settings\...\1126.jpg "
Dim i As Image = Image.FromFile(imageName)
Dim g As Graphics = Graphics.FromImage(i) '此处从背景图创建Greaphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'划线
Dim BluePen As New Pen(Color.Blue, 5)
BluePen.DashStyle = Drawing2D.DashStyle.Solid
g.DrawLine(BluePen, 100.0F, 170, 500.0F, 170)
g.Dispose()
PictureBox1.Image = i
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'退出
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'存盘
i.Save( "C:\testimage.jpg ", ImageFormat.Jpeg)
i.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.Image = i
End Sub
End Class
dim bmp as new bitmap(width,height)dim g as graphics=graphics.fromimage(bmp)g.drawlines(pen.blue,20,20,100,20)backgroundimage=bmp
可以把所有画的线都保存在一个列表中,画的时候全部画出即可。如下:
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
运行效果:
1. 创建一个Graphics对象实例。
绘制图形必须创建Graphics对象。如果是在窗体上绘图,要使用下列代码创建Graphics对象;
Dim MyGraphics As Graphics = Me.CreateGraphics
如果是在PictrueBox里绘图,要使用下列代码创建Graphics对象;
Dim MyGraphics As Graphics = PictureBox1.CreateGraphics
2. 定义一个Brush对象,用来填充图形(如果你需要填充的话)。
如果填充封闭曲线或者多边形,必须创建Brush对象(或者Brush类的继承类对象),用来确定填充的颜色。例如下面的代码,创建了一个填充红色的画刷对象。在最后的括号里,用Color结构指定的枚举值,确定画刷的颜色。限于篇幅有关Color结构这里不展开,可能在后续博文里介绍。
Dim RedBrush As New SolidBrush(Color.Red)
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款