Why I can draw this image? This is my code.
-
I wrote two functions to draw something like this. __ __ _____ __ __ _____ __ __ _____ Have six lines and two kinds. The Yao fuction draws ____ or __ __ and the DoubleGua one send a string like "010101" to Yao to draw lines. However, I cannot draw six lines. Only one line show on my form. Where am I wrong? Can't this funtion run? The function is as below: Public Function DoubleGua(ByVal GuaIndex As String) As Bitmap Dim Lx As Integer = 7 Dim myBmp As New Bitmap(40, 42) Dim g As Graphics = Graphics.FromImage(myBmp) Dim i As Byte For i = 0 To 5 Dim Lpoint As New Point(Lx * i, 0) Dim tmpyao As String = GuaIndex.Substring(i, 1) g.DrawImage(Yao(tmpyao, Color.Black), Lpoint) Next Return myBmp End Function Public Function Yao(ByVal PorN As String, ByVal _Color As Color) As Bitmap Dim myBmp As New Bitmap(40, 5) Dim g As Graphics = Graphics.FromImage(myBmp) Dim Guabrush As SolidBrush = New SolidBrush(_Color) Select Case PorN Case "0" Dim rects As Rectangle() = {New Rectangle(0, 0, 18, 5), New Rectangle(22, 0, 18, 5)} g.FillRectangles(Guabrush, rects) Case "1" Dim rects As Rectangle() = {New Rectangle(0, 0, 40, 5)} g.FillRectangles(Guabrush, rects) End Select Return myBmp End Function
-
I wrote two functions to draw something like this. __ __ _____ __ __ _____ __ __ _____ Have six lines and two kinds. The Yao fuction draws ____ or __ __ and the DoubleGua one send a string like "010101" to Yao to draw lines. However, I cannot draw six lines. Only one line show on my form. Where am I wrong? Can't this funtion run? The function is as below: Public Function DoubleGua(ByVal GuaIndex As String) As Bitmap Dim Lx As Integer = 7 Dim myBmp As New Bitmap(40, 42) Dim g As Graphics = Graphics.FromImage(myBmp) Dim i As Byte For i = 0 To 5 Dim Lpoint As New Point(Lx * i, 0) Dim tmpyao As String = GuaIndex.Substring(i, 1) g.DrawImage(Yao(tmpyao, Color.Black), Lpoint) Next Return myBmp End Function Public Function Yao(ByVal PorN As String, ByVal _Color As Color) As Bitmap Dim myBmp As New Bitmap(40, 5) Dim g As Graphics = Graphics.FromImage(myBmp) Dim Guabrush As SolidBrush = New SolidBrush(_Color) Select Case PorN Case "0" Dim rects As Rectangle() = {New Rectangle(0, 0, 18, 5), New Rectangle(22, 0, 18, 5)} g.FillRectangles(Guabrush, rects) Case "1" Dim rects As Rectangle() = {New Rectangle(0, 0, 40, 5)} g.FillRectangles(Guabrush, rects) End Select Return myBmp End Function
astcws wrote:
Dim Lpoint As New Point(Lx * i, 0)
You're offsetting the X coordinate, not the Y. This is drawing each line of your image on the same line, just moved to the right, not down. It should be:
Dim Lpoint As New Point(0, Lx \* i)
Also, you MUST
Dispose()
your Graphics objects after you're done using them. You'll eventually get OutOfMemory exceptions if you don't, and/or you're system will start doing crazy things. If you create the Graphics object, you have the dispose of it. The same is true for Brush's, Pen's, and just about any other object that has a Dispose method.Public Function Yao(ByVal PorN As String, ByVal _Color As Color) As Bitmap
Dim myBmp As New Bitmap(40, 5)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim Guabrush As SolidBrush = New SolidBrush(_Color)
Select Case PorN
Case "0"
Dim rects As Rectangle() = {New Rectangle(0, 0, 18, 5), New Rectangle(22, 0, 18, 5)}
g.FillRectangles(Guabrush, rects)
Case "1"
Dim rects As Rectangle() = {New Rectangle(0, 0, 40, 5)}
g.FillRectangles(Guabrush, rects)
End Select
g.Dispose()
Return myBmp
End FunctionDave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
astcws wrote:
Dim Lpoint As New Point(Lx * i, 0)
You're offsetting the X coordinate, not the Y. This is drawing each line of your image on the same line, just moved to the right, not down. It should be:
Dim Lpoint As New Point(0, Lx \* i)
Also, you MUST
Dispose()
your Graphics objects after you're done using them. You'll eventually get OutOfMemory exceptions if you don't, and/or you're system will start doing crazy things. If you create the Graphics object, you have the dispose of it. The same is true for Brush's, Pen's, and just about any other object that has a Dispose method.Public Function Yao(ByVal PorN As String, ByVal _Color As Color) As Bitmap
Dim myBmp As New Bitmap(40, 5)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim Guabrush As SolidBrush = New SolidBrush(_Color)
Select Case PorN
Case "0"
Dim rects As Rectangle() = {New Rectangle(0, 0, 18, 5), New Rectangle(22, 0, 18, 5)}
g.FillRectangles(Guabrush, rects)
Case "1"
Dim rects As Rectangle() = {New Rectangle(0, 0, 40, 5)}
g.FillRectangles(Guabrush, rects)
End Select
g.Dispose()
Return myBmp
End FunctionDave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007