Results 1 to 6 of 6

Thread: [2008] Creating a finer Pen

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    [2008] Creating a finer Pen

    I am working on a part of an application that requires a graph which i have done using a picturebox and CreateGraphics.DrawLine, it accepts scores which there could be between 20 and 45 scores to show on the graph across the width of the picturebox. My question is, can we draw a line with a finer pen so the diagonal lines dont look to jagged (see pictures).

    20 scores is probably ok but when it accepts 45 it looks a bit dodgy.
    Attached Images Attached Images   

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2008] Creating a finer Pen

    A Pens width can only be as small as 1. You could add AntiAliasing smoothing so its not all jaggged.

    System.Drawing.Drawing2D.SmoothingMode.AntiAlias as well as System.Drawing.Drawing2D.SmoothingMode.HighQuality
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Creating a finer Pen

    Please tell me that you haven't actually used CreateGraphics.DrawLine in your code. If you ever do call CreateGraphics, which should be a very, VERY rare thing, then you need to keep a reference to the Graphics object it creates so that you can dispose it, which you should almost always do with a Using block:
    vb.net Code:
    1. Using g As Graphics = myControl.CreateGraphics()
    2.     'Use g here.
    3. End Using
    The reason that you should almost never need to call CreateGraphics is that you should do all your drawing in the Paint event handler or OnPaint method of the control, which already has a Graphics object provided via the PaintEventArgs object you get from the 'e' parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Creating a finer Pen

    It sounds like i am doing this completely wrong, the code as follows is a sub within a module that accepts the picturebox, maximum score and the scores. Could you point out what i shouldnt be doing.

    I am not being ignorant by not replying straight away, i need to go offline until tomorrow, thanks for having a look.
    Code:
     Public Sub GraphSub(ByVal pb As PictureBox, ByVal pbMaxH As Integer, ByVal Scores As Integer())
    
            Dim MaxSplit As Double, WidSplit As Double, ScH As Double
            Dim LastPoint As New Point
            Dim LastX As Integer, LastY As Integer
    
            pb.CreateGraphics.Clear(pb.BackColor)
            MaxSplit = pb.Height / pbMaxH
    
            For i As Integer = 0 To Scores.GetUpperBound(0)
    
                ScH = Scores(i) * MaxSplit
                WidSplit = Math.Round((pb.Width - LastPoint.X) / ((Scores.Length + 1) - i))
    
                LastPoint.X = CInt(LastPoint.X + WidSplit)
                LastPoint.Y = CInt(Math.Round(pb.Height - ScH))
    
                pb.CreateGraphics.DrawLine(Pens.Red, LastPoint.X, LastPoint.Y, LastPoint.X, pb.Height)
                pb.CreateGraphics.DrawRectangle(Pens.Red, LastPoint.X - 2, LastPoint.Y - 2, 4, 4)
    
                pb.CreateGraphics.DrawLine(Pens.Blue, LastX, LastY, LastPoint.X, LastPoint.Y)
    
                LastY = LastPoint.Y
                LastX = LastPoint.X
    
            Next
    
        End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Creating a finer Pen

    You're calling CreateGraphics four times, so you are creating four different Graphics objects and disposing none of them. That's a resource leak right there. Apart from that, the fact that you're not doing your drawing on the Paint event of the PictureBox means that, as soon as the PictureBox gets repainted, like when it's minimised and restored, your graph will simply disappear.

    What you need to do is store all the variable data for the drawing in member variables somewhere. When you want to draw the graph you set those variables if they haven't been set already and then force the Picturebox to repaint itself by calling its Refresh method. That will raise the PictureBox's Paint event and you do your drawing in the PictureBox's Paint event handler.

    Follow the Simple Drawing link in my signature for an example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Creating a finer Pen

    Thank you for your time, ill look into that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width