2

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;
        }
        ...
}
Filus1025
  • 43
  • 1
  • 7
  • 3
    First of all your explicit operator won't compile. You can't add a explicit conversion operator in another class. It should be in either one of the types. – Sriram Sakthivel May 10 '15 at 13:50

2 Answers2

4

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();
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
0

U could try this

private static System.Drawing.PointF convertToPointF(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 = convertToPointF(blob.CenterOfGravity);
    }
Jur Clerkx
  • 698
  • 4
  • 14