Search code examples
c#xna

'System.Drawing.RectangleF' does not contain a definition for 'Center'


I get this error message in the following line:

Point posDiff = new Point(RectF1.Center.X - RectF2.Center.X, RectF1.Center.Y - RectF2.Center.Y);

'System.Drawing.RectangleF' does not contain a definition for 'Center' and no extension method 'Center' accepting a first argument of type 'System.Drawing.RectangleF' could be found (are you missing a using directive or an assembly reference?)

What is wrong? Which reference is missing?


Solution

  • Quite simply, it's right. Look at the documentation for RectangleF. There's no Center property. It's not clear why you think there is one. Were you getting confused with another type, perhaps?

    Of course, it's easy enough to create your own method to return the center of a RectangleF, and you could make that an extension method:

    public static PointF Center(RectangleF rectangle)
    {
        return new PointF(rectangle.X + rectangle.Width / 2,
                          rectangle.Y + rectangle.Height / 2);
    }
    

    It's not clear why you're trying to create a Point (int-based) from values in a RectangleF (float-based) though. I could imagine this:

    PointF center1 = RectF1.Center(); // Using the new extension method
    PointF center2 = RectF2.Center();
    PointF posDiff = new PointF(center1.X - center2.X, center1.Y - center2.Y);
    

    ... although I'd be tempted to create a VectorF type to make it clear that you're logically talking about the difference between two points, not a new point.