[RESOLVED] Placing Graphics Functionality into a library class
I have created a library class in VB Net (Framework). I have this class include a method to plot the class object. In the main program I have added the library class as an existing item and I have created a Graphics object that I pass to the library class method. However, in the library class method, the passed graphics object does not seem to contain the functionality to plot, say lines or rectangles, but rather shows as an error. How can I do this?
Re: Placing Graphics Functionality into a library class
Quote:
Originally Posted by
KeithMcCloy
I have created a library class in VB Net (Framework). I have this class include a method to plot the class object. In the main program I have added the library class as an existing item and I have created a Graphics object that I pass to the library class method. However, in the library class method, the passed graphics object does not seem to contain the functionality to plot, say lines or rectangles, but rather shows as an error. How can I do this?
Might help if you told us what the error is, there are a lot of possible errors and the solution will obviously depend on the error.
Re: Placing Graphics Functionality into a library class
The Graphics object can only function when it has a surface to draw on - in other words, a specific Control or Bitmap. I don't think you can pass it as an argument to another class without losing connection to the drawing surface, even when there is no error. A simple simple solution would be to pass a Bitmap to the class instead of a Graphics object. Then generate the Graphics within the class using Graphics.FromImage.
EDIT: correction -- you could pass the Control or a Bitmap to be drawn on as an additional argument. Instead you might consider recasting the methods in your class as a set of Extension Methods to the Graphics object.
BB
Re: Placing Graphics Functionality into a library class
Thanks PD and BB for your response. My class is a VB Net (Foundation) library class. At the start is the following code;-
Imports System.Drawing
Namespace System.Drawing
Public Class Point
End Class
Public Class Color
End Class
Public Class Graphics
End Class
Public Class Graphics(Of DrawLine)
End Class
Public Class Pen
End Class
End Namespace
I understand the need for the Imports statement, but I do not understand the need for the Namespace definition, nor its components.
In the body of my class I have a Sub to plot the class object that happens to be a stitch of knitting;-
Case 1 'Knit stitches
numpts = symbolspecs(0, 0)
ReDim plotsymbol(numpts - 1)
Euclid2D.TransformSymbolToScreenCoords(skale, centrepoint, symbols, symbolspecs, 0, plotsymbol, cps, cpr, totalorient)
mygraphics.DrawLine(mypen, plotsymbol(0).X, plotsymbol(0).Y, plotsymbol(1).X, plotsymbol(1).Y)
mygraphics.DrawLine(mypen, plotsymbol(1).X, plotsymbol(1).Y, plotsymbol(2).X, plotsymbol(2).Y)
Case 2 'Purl stitches
mygraphics is the System.Drawing.Graphics object created in the operating program to represent the graphics surface of a PaintBox. It is passed without error. mypen is a System.Drawing.Pen object that is passed without error and it does not show as an error in my code. plotsymbol is an array of type System.Drawing.Point that is created without error in this Sub. The mygraphics.DrawLine, plotsymbol().X and plotsymbol().Y values are underlined in red. mygraphics.DrawLine has the error BC30456, "DrawLIne" is not a member of "Graphics". If I start a new line of code with mygraphics. I get four options, (Equals, GetHashCode, GetType and ToString), yet I have had this line working in an earlier version from several years ago. Maybe DrawLIne has been replaced in Foundation with something else, in which case I do not know what it is at this stage. plotsymbol() is of type System.Drawing.Point, which has X and Y as variables, yet it is attracting the error BC30456 "X is not a member of "Point". Your advice would be appreciated.
Re: Placing Graphics Functionality into a library class
Have you added a reference to System.Drawing? If not you will need to do that, you should also remove the lines
Code:
Namespace System.Drawing
Public Class Point
End Class
Public Class Color
End Class
Public Class Graphics
End Class
Public Class Graphics(Of DrawLine)
End Class
Public Class Pen
End Class
End Namespace
as with them you are declaring your own versions of the Classes, that is why you are getting those errors.
Re: Placing Graphics Functionality into a library class
Quote:
Originally Posted by
KeithMcCloy
I do not understand the need for the Namespace definition
There's a reason for that: there is no need. Get rid of it. When you create a VB project, the default namespace is the same as the project name. You can change that in the project properties if you want. You only need a namespace declaration in your code if you want a type to be in a child namespace of that default namespace. You don't.
This is pretty simple stuff. Your class is going to draw and it needs a Graphics object to do that with. You don't create that object. It will come from the calling code. It will either be from the e.Graphics property in a Paint event handler or OnPaint method or it will be the result of a call to Graphics.FromImage. Those are basically the only two ways you get a Graphics object. You could call CreateGraphics in a control but you pretty much never should.
So, you could have a Graphics parameter on each of your methods, e.g.
vb.net Code:
Public Class MyGraphicsClass
Public Sub DrawSomething(g As Graphics)
'Use g here.
End Sub
Public Sub DrawSomethingElse(g As Graphics)
'Use g here.
End Sub
End Class
Alternatively, you can pass the Graphics object into the class once and use it each time:
vb.net Code:
Public Class MyGraphicsClass
Private g As Graphics
Public Sub New(g As Graphics)
Me.g = g
End Sub
Public Sub DrawSomething()
'Use g here.
End Sub
Public Sub DrawSomethingElse()
'Use g here.
End Sub
End Class
Re: Placing Graphics Functionality into a library class
I greatly appreciate the suggestions from PD and JMc, they have been very helpful, but my problem which I suspect is simplified, is not yet quite solved.
Imports System.Drawing
Public Class Stitch
'Implements IDisposable
'The stitch class contains details of a stitch
' type is the type of knitting in this row; -
' -1 = unknown
' 0 = none or gap
' 1 = knit stitch
' 2 = purl stitch
' 3 = CastOn in Knit,
' 4 = CastOn in Purl
' 5 = CastOff in knit
' 6 = CastOff in purl
' 7 = Slip Stitch in knit
' 8 = Slip Stitch in purl
' 9 = Yarn Over in knit
' 10 = Yarn Over in purl
'
' colour is the colour assigned to the stitch.
' location is the location (X, Y) of the centre of the stitch in global coordinates.
' There are three potential coordinate systems; -
' local coordinates to the figure within the patch; used to do many computations in relation to the patch figure.
' global coordinates for the whole piece of knitting, made up of several patches. It is these coordinates that are stored within a stitch.
' Both of these coordinate systems obey the standard rules of coordinate geometry (x to right, y up).
' Screen coordinates used to display a patch, or a garment on the screen. They obey the screen coordinate geometry (x to right, y down).
' The stitch orientation is the local orientation of the stitch relative to the local row axis, in radians. For standard knitting, the orientation
' is usualy set to zero. For in the round knitting, the stitch orientation is unique to each stitch around the figure.
' orientation values are in radians.
' spc, rpc and yps are stitch/cm, rows/cm and yarn læength (cm) per stitch.
'
Public type As Integer
Public colour As Color
Public location As Euclid2D
Public orientation As Double
Public spc, rpc, yps As Double 'stitches/cm, rows/cm, yarn used/stitch(cm)
'-----------------------------------------------------------------------------------------------------------------------------
Public Sub New(ByVal newtype As Integer)
'NewST01: Default call to New
type = newtype 'Currently type can be 1 (Stockinette Stitch), 2 (Garter Stitch), 3 (Cast On)
colour = New System.Drawing.Color
location = New Euclid2D(0, 0, "cm", 0.0, 0.0, 0.0, 0.0)
orientation = 0.0
spc = 0.4
rpc = 0.5
yps = 2.0
End Sub
In the attached code above, you can see that I have the Imports System.Drawing command at the start of the class. Despite this, I now get BC30002 errors whenever I refer to an attribute or a method in the System.Drawing Namespace such as .Graphics; .Pen; .Point and .Color. In the above code, this occurs on the line "Public colour As Color" and colour = New System.Drawing.Color and elsewhere where I refer to attributes or methods of System.Drawing. I agree that this should be very simple, but so far, I have not been able to sort it out.
Re: Placing Graphics Functionality into a library class
Have you added a reference to the System.Drawing.dll as well? If you want to use classes defined in a classlibrary you need to reference that library.
Re: Placing Graphics Functionality into a library class
Quote:
Originally Posted by
KeithMcCloy
I now get BC30002 errors
Rather than giving us just the error code and expecting us all to search for what that means for ourselves, you should provide the actual error message here. ALWAYS provide ALL the relevant information. We don't all remember what every error code means.
Quote:
Originally Posted by
PlausiblyDamp
Have you added a reference to the System.Drawing.dll as well? If you want to use classes defined in a classlibrary you need to reference that library.
Based on the error code, that is indeed the issue. Based on this seeming lack of understanding I recommend that the OP reads my blog post on Assemblies & Namespaces.
Re: Placing Graphics Functionality into a library class
Fixed, finally!! Thanks very much, JMc, PD and bb. I will close this now.