Hello I need some help :) I have my custom class Filters and inside of it I defined explicit conversion operator to convert from AForge.Point to System.Drawing.PointF both AForge.Point and System.Drawing.PointF are structs from libraries. blob.CenterOfGravity is type of AForge.Point. The problem is that intelisense is telling me that "Cannot convert 'AForge.Point' to 'System.Drawing.PointF'. I don't know why this conversion can't be done :/. Thanks for all replies.
class Filters
{
public static explicit operator System.Drawing.PointF(AForge.Point apoint)
{
return new PointF(apoint.X,apoint.Y);
}
public void DrawData(Blob blob, Bitmap bmp)
{
int width = blob.Rectangle.Width;
int height = blob.Rectangle.Height;
int area = blob.Area;
PointF cog = (PointF)blob.CenterOfGravity;
}
...
}
You can't do this using an operator
as these have to be defined by the types you are converting (i.e. AForge.Point
or System.Drawing.PointF
). Per the documentation:
Either the type of the argument to be converted, or the type of the result of the conversion, but not both, must be the containing type.
One alternative is to define an extension method for AForge.Point
:
public static class PointExtensions
{
public static PointF ToPointF(this AForge.Point source)
{
return new PointF(source.X, source.Y);
}
}
And use like this:
PointF cog = blob.CenterOfGravity.ToPointF();