Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Why I can draw this image? This is my code.

Why I can draw this image? This is my code.

Scheduled Pinned Locked Moved Visual Basic
graphicsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astcws
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • A astcws

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 Function

      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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 Function

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        astcws
        wrote on last edited by
        #3

        Yeah, what a stupid mistake? As the method "disposed"...I really don't know it before... Thanks very much.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups