Maconel(Maconel) 我的意思是说你用一个像素宽度画不出没有锯齿的线,需要做边缘模糊处理。中午抽空写了一断代码,画了三条线条,大家可以比较一下视觉效果。因为没有时间去做高斯模糊的代码用了一个变相的办法,效果不是很好但是应该能说明一些问题吧创建一个窗体,粘下面的代码:Option Explicit Const x1 = 100 Const x2 = 2500 Const y1 = 100 Const y2 = 250Private Sub Form_Load() Dim i As Integer '第一条逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5 For i = 10 To 1 Step -1 ForeColor = RGB(i * 19, i * 19, i * 19) DrawWidth = i Line (x1, y1)-(x2, y2) Next i '宽度为10的线,直接画 DrawWidth = 10 Line (x1, y1 + 500)-(x2, y2 + 500) '宽度为1线,直接画 DrawWidth = 1 Line (x1, y1 + 1000)-(x2, y2 + 1000) End Sub
哦,忘了说了,要把form的autoredraw设为true 修改了一下代码,这下应该没有问题了Option Explicit Const x1 = 100 Const x2 = 2500 Const y1 = 100 Const y2 = 250Private Sub Form_Load() Dim i As Integer Form1.AutoRedraw = True '第一条逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5 For i = 10 To 1 Step -1 ForeColor = RGB(i * 19, i * 19, i * 19) DrawWidth = i Line (x1, y1)-(x2, y2) Next i '宽度为10的线,直接画 DrawWidth = 10 Line (x1, y1 + 500)-(x2, y2 + 500) '宽度为1线,直接画 DrawWidth = 1 Line (x1, y1 + 1000)-(x2, y2 + 1000) End Sub
呵呵,在上班时间偷偷写了另一种办法,利用视觉误差原理。当然,和真正科学的抗锯齿算法相比,我的这些办法显得不够专业了,所以效果也不是很好。谁有兴趣可以继续完善。代码:Option Explicit Const x1 = 100 Const x2 = 2500 Const y1 = 100 Const y2 = 250Private Sub Form_Load() Dim i As Integer Me.Show Form1.AutoRedraw = True'宽度为5的线,直接画 DrawWidth = 5 Line (x1, y1)-(x2, y2)
'逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5 For i = 10 To 1 Step -1 ForeColor = RGB(i * 19, i * 19, i * 19) DrawWidth = i Line (x1, y1 + 500)-(x2, y2 + 500) Next i'宽度为2线,直接画 DrawWidth = 2 Line (x1, y1 + 1000)-(x2, y2 + 1000)'第4条线,用视觉误差方法画出来的,线宽3,视觉应该在2左右 '如果要投入实用可能还要做更多的工作,看大家有没有兴趣继续完善这段代码了 Dim sX As Integer Dim sY As Integer Dim j As Double sX = x1 sY = y1 + 1500 DrawWidth = 1 For i = 0 To 10 ForeColor = RGB(0, 0, 0) myLine sX + i * 240, sY + i * 15, 240, 0 myLine sX + i * 240, sY + (i + 1) * 15, 240, 2 myLine sX + i * 240, sY + (i - 1) * 15, 240, 1 Next End Sub Private Sub myLine(mX1 As Integer, mY1 As Integer, mLenth As Integer, mStyle As Integer) Dim i As Double Dim a As Integer Select Case mStyle Case 0 ForeColor = RGB(0, 0, 0) Line (mX1, mY1)-(mX1 + mLenth, mY1) Case 1 For i = 1 To mLenth a = i / mLenth * 192 ForeColor = RGB(a, a, a) Line (mX1 + i - 1, mY1)-(mX1 + i, mY1) Next Case 2 For i = 1 To mLenth a = 192 - i / mLenth * 192 ForeColor = RGB(a, a, a) Line (mX1 + i - 1, mY1)-(mX1 + i, mY1) Next End Select End Sub最后说一句: 楼主啊,多给点分吧,赚分不容易啊!
看看planet-source-code上的一个例子,这是说明,下载地址在最下面。 Smooth Anti-alias Lines and Polygons Optimized class for drawing smooth anti-aliased lines (choose GetPixel/SetPixelV or DIBits method) based on TMT Pascal/ASM code by Jonas Widarsson. This goes beyond Gupta-Sproull (enhanced Bresenham) algorithm. If you抮e tired of the jaggies this may be just the ticket. Lines are thicker than Vb lines due to the blending on each side of line (a small price to pay for nice lines). Demo shows how to create an analog clock using anti-aliased polygon clock hands. Include the class in your project (see demo code) or compile to a DLL and add reference, See Screenshot. http://www.planet-source-code.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=33588&strZipAccessCode=moot335888604
但是要用.net做。
倒是可以考虑photoshop的方法,线条边缘用类似高斯模糊的办法将锯齿边缘扩散掉就比较不容易看出锯齿了。
我的意思是说你用一个像素宽度画不出没有锯齿的线,需要做边缘模糊处理。中午抽空写了一断代码,画了三条线条,大家可以比较一下视觉效果。因为没有时间去做高斯模糊的代码用了一个变相的办法,效果不是很好但是应该能说明一些问题吧创建一个窗体,粘下面的代码:Option Explicit
Const x1 = 100
Const x2 = 2500
Const y1 = 100
Const y2 = 250Private Sub Form_Load()
Dim i As Integer
'第一条逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5
For i = 10 To 1 Step -1
ForeColor = RGB(i * 19, i * 19, i * 19)
DrawWidth = i
Line (x1, y1)-(x2, y2)
Next i
'宽度为10的线,直接画
DrawWidth = 10
Line (x1, y1 + 500)-(x2, y2 + 500)
'宽度为1线,直接画
DrawWidth = 1
Line (x1, y1 + 1000)-(x2, y2 + 1000)
End Sub
修改了一下代码,这下应该没有问题了Option Explicit
Const x1 = 100
Const x2 = 2500
Const y1 = 100
Const y2 = 250Private Sub Form_Load()
Dim i As Integer
Form1.AutoRedraw = True
'第一条逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5
For i = 10 To 1 Step -1
ForeColor = RGB(i * 19, i * 19, i * 19)
DrawWidth = i
Line (x1, y1)-(x2, y2)
Next i
'宽度为10的线,直接画
DrawWidth = 10
Line (x1, y1 + 500)-(x2, y2 + 500)
'宽度为1线,直接画
DrawWidth = 1
Line (x1, y1 + 1000)-(x2, y2 + 1000)
End Sub
Const x1 = 100
Const x2 = 2500
Const y1 = 100
Const y2 = 250Private Sub Form_Load()
Dim i As Integer
Me.Show
Form1.AutoRedraw = True'宽度为5的线,直接画
DrawWidth = 5
Line (x1, y1)-(x2, y2)
'逐步扩散模糊,模拟高斯模糊抗锯齿,线宽10,视觉宽度应该是5
For i = 10 To 1 Step -1
ForeColor = RGB(i * 19, i * 19, i * 19)
DrawWidth = i
Line (x1, y1 + 500)-(x2, y2 + 500)
Next i'宽度为2线,直接画
DrawWidth = 2
Line (x1, y1 + 1000)-(x2, y2 + 1000)'第4条线,用视觉误差方法画出来的,线宽3,视觉应该在2左右
'如果要投入实用可能还要做更多的工作,看大家有没有兴趣继续完善这段代码了
Dim sX As Integer
Dim sY As Integer
Dim j As Double
sX = x1
sY = y1 + 1500
DrawWidth = 1
For i = 0 To 10
ForeColor = RGB(0, 0, 0)
myLine sX + i * 240, sY + i * 15, 240, 0
myLine sX + i * 240, sY + (i + 1) * 15, 240, 2
myLine sX + i * 240, sY + (i - 1) * 15, 240, 1
Next
End Sub
Private Sub myLine(mX1 As Integer, mY1 As Integer, mLenth As Integer, mStyle As Integer)
Dim i As Double
Dim a As Integer
Select Case mStyle
Case 0
ForeColor = RGB(0, 0, 0)
Line (mX1, mY1)-(mX1 + mLenth, mY1)
Case 1
For i = 1 To mLenth
a = i / mLenth * 192
ForeColor = RGB(a, a, a)
Line (mX1 + i - 1, mY1)-(mX1 + i, mY1)
Next
Case 2
For i = 1 To mLenth
a = 192 - i / mLenth * 192
ForeColor = RGB(a, a, a)
Line (mX1 + i - 1, mY1)-(mX1 + i, mY1)
Next
End Select
End Sub最后说一句:
楼主啊,多给点分吧,赚分不容易啊!
县按照理论用PSet画点,但是点的坐标是“量子化的”,取整了就有误差有锯齿,然后再判断实际点的位置到理想直线的距离,并以此设置一个Alpha值。不知效果如何^_^
Smooth Anti-alias Lines and Polygons Optimized class for drawing smooth anti-aliased lines (choose GetPixel/SetPixelV or DIBits method) based on TMT Pascal/ASM code by Jonas Widarsson. This goes beyond Gupta-Sproull (enhanced Bresenham) algorithm. If you抮e tired of the jaggies this may be just the ticket. Lines are thicker than Vb lines due to the blending on each side of line (a small price to pay for nice lines). Demo shows how to create an analog clock using anti-aliased polygon clock hands. Include the class in your project (see demo code) or compile to a DLL and add reference, See Screenshot.
http://www.planet-source-code.com/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=33588&strZipAccessCode=moot335888604