Romy
Romy

Reputation: 407

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

Upvotes: 0

Views: 2100

Answers (2)

Geoff James
Geoff James

Reputation: 3180

You're using the DrawRectangles() method of System.Drawing.Graphics, which expects an array of Rectangles.

Use the singular version: DrawRectangle():

G.DrawRectangle(pen5, rect2.Left, rect2.Top, rect2.Width, rect2.Height); // Singular

MSDN gives you (a lot) of information about the Graphics class.

Hope this helps!

Upvotes: 2

DavidG
DavidG

Reputation: 119076

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 });
    

Upvotes: 2

Related Questions