Search code examples
c#graphicdrawrectanglegraphicspath

Drawing getbounds rectangle or rectangleF?


I have a List with float coordinates and I'm trying to draw a rectangle around a line previously drawn.

Graphics G = e.Graphics;    
Pen pen5 = new Pen(Color.DodgerBlue, 0.01f);
var rect2 =  new RectangleF();

GraphicsPath maliOkvir = new GraphicsPath();
maliOkvir.AddLine(((float)(odabraniSegment[0].startX)),
                   (float)(odabraniSegment[0].startY),
                   (float)(odabraniSegment[0].endX),
                   (float)(odabraniSegment[0].endY));

rect2 = maliOkvir.GetBounds();
G.DrawRectangle(pen5, rect2);

I'm getting an error on rect2 part:

G.DrawRectangles(pen5, rect2);

Cannot convert from 'System.Drawing.RectangleF' to 'System.Drawing.RectangleF[]'

How can I fix this? tried multiple variations of Rectangle and RectangleF, none works together.. the end result should look like this:

enter image description here


Solution

  • The DrawRectangles method expects an array of Rectangle or RectangleF objects but you are only passing in a single item. You should either:

    1. Switch to use the singular version of the method, i.e. DrawRectangle
    2. Pass in an array:

      G.DrawRectangles(pen5, new [] { rect2 });