|
-
Jan 4th, 2009, 06:10 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 4th, 2009, 06:15 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 4th, 2009, 06:36 PM
#3
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:
Using g As Graphics = myControl.CreateGraphics() 'Use g here. 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.
-
Jan 4th, 2009, 06:53 PM
#4
Thread Starter
Hyperactive Member
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
-
Jan 4th, 2009, 07:10 PM
#5
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.
-
Jan 5th, 2009, 12:06 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|