0

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

Romy
  • 407
  • 6
  • 25
  • 1
    At first glance, it looks like whatever `e` is, has the method `DrawRectangles` in it - which alludes to the fact it might take an array of `RectangleF`s (`RectangleF[]`) - hence your error. Looking at the `Graphics` Class, it has a `.DrawRectangle` (singular) method: https://msdn.microsoft.com/en-us/library/sx8yykw8(v=vs.110).aspx Are you sure you're using the right method? – Geoff James Jun 20 '16 at 23:34
  • 1
    `DrawRectangles` expects an array of rectangles, you probably want `DrawRectangle` (without the s on the end) – DavidG Jun 20 '16 at 23:36
  • @DavidG you're right, my bad, it was a leftover from previous attempts to fix it. – Romy Jun 20 '16 at 23:40
  • So your issue is fixed now? – DavidG Jun 20 '16 at 23:42
  • Nope, the same error remains. – Romy Jun 20 '16 at 23:43
  • Exactly the same error? That's not possible. – DavidG Jun 20 '16 at 23:44
  • @DavidG Exactly the same. The `DrawRectangle` command doesn't want float values but my `rect2` can only be made to `RectangleF`, if i remove the F, new error comes up on the `GetBounds` command because it also needs `float` values. – Romy Jun 20 '16 at 23:46
  • 1
    Well like I said in my answer, you can also use option 2 – DavidG Jun 20 '16 at 23:51

2 Answers2

2

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!

Penguin
  • 311
  • 2
  • 14
Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • A mistake on my part, it should have been DrawRectangle, I only need one, but I just need the functions to all take float values, so the thing can work :) – Romy Jun 20 '16 at 23:43
  • Glad to have helped @Romy! Easy mistake to make. Don't forget to mark as answer when you're done. Happy programming :) – Geoff James Jun 20 '16 at 23:50
  • Although the extra "s" was a mistake, the same problem still remains :/ – Romy Jun 20 '16 at 23:51
  • *Exactly* the same error? That's not possible. Have you tried cleaning and rebuilding your solution? – Geoff James Jun 20 '16 at 23:53
  • Yea weird, but DavigG suggested sticking with the array way, and that worked. :) – Romy Jun 20 '16 at 23:56
  • That was going to be my next suggestion of passing an array with only the `rect2` in it, like David suggested. Glad you got it working :) – Geoff James Jun 20 '16 at 23:57
  • Worth noting that you can pass in a `Rectangle`, but not a `RectangleF` (as is provided in the original question). For `RectangleF`, you have to pass `X`, `Y`, `Width`, and `Height` as individual arguments to [this overload of Graphics.DrawRectangle](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.drawrectangle?view=dotnet-plat-ext-6.0#system-drawing-graphics-drawrectangle(system-drawing-pen-system-single-system-single-system-single-system-single)) – Wyck May 02 '22 at 17:18
2

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 });
    
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    or 3. Pass in the float values: `g.DrawRectangle(pen5, rect2.X, rect2.Y, rect2.Width, rect2.Height);` :-) – LarsTech Jun 20 '16 at 23:57
  • Indeed @LarsTech, was tempted to add that one, but it's always a PITA to remember the parameter order. – DavidG Jun 20 '16 at 23:58