'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:
Solution 1:[1]
The DrawRectangles
method expects an array of Rectangle
or RectangleF
objects but you are only passing in a single item. You should either:
- Switch to use the singular version of the method, i.e.
DrawRectangle
Pass in an array:
G.DrawRectangles(pen5, new [] { rect2 });
Solution 2:[2]
You're using the DrawRectangles()
method of System.Drawing.Graphics
, which expects an array of Rectangle
s.
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!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DavidG |
Solution 2 | Penguin |