0

Hi,

This may be a simple issue, but im gettin nowhere trying to fix it.
I have a form with a picturebox, the picturebox shows a map of our greenhouses. I want to be able to create clickable areas on the image.
I have created a class to store each area, and can create them fine.
The issue comes in trying to draw the areas onto the picture.
Heres what i have:

Bay _bay = new Bay();
                _bay.Points = newBay;
                _bay.Color = Color.Red;
                _bay.Highlight = false;
                Bays.Add(_bay);
                pictureBox1.Invalidate();

each clickable area will be a bay in the greenhouse. I create an instance of the Bay class, instantiate it with a set of 4 PointF values (newBay) which denote the area.
next:

void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            foreach (Bay b in Bays)
            {
                b.Draw(e.Graphics);
            }
        }

each Bay's draw method is called...

public void Draw(Graphics g)
        {
            SolidBrush b = new SolidBrush(this.Color);
            g.FillPolygon(b, _points);
            b.Dispose();

            if (Highlight)
            {
                Pen p = new Pen(Color.Red, 3);
                p.DashStyle = DashStyle.DashDot;
                g.DrawPolygon(p, _points);
                p.Dispose();
            }
        }

My problem is that only the LAST polygon is showing up : / i think the Graphics object may be overwritten by each call...but i cant figure out why or how to stop it...someone help..please! :)

2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by ddanbe
0

Don't know if it helps, but have you ever tried to remove(comment them out) all the Dispose methods from your Draw method?

0

hmmm...how typical is that...i spent ALL day on this and get nowhere...as soon as i put it in a post i make a breakthrough : /

Its NOT a graphics issue at all...all of the polygons are being drawn!!
I had a random insight and checked the collection of Bays to see if there was something odd going on there, and there is. Each time i add a new bay the old bay's have their _points changed to match :/
I think i may inadvertantly be storing a reference instead of a copy when i set the instance varaible...can anyone confirm that?

PointF[] _points;

 public Bay()
        {
            _points = new PointF[4];
        }

 public PointF[] Points
        {
            get { return _points; }
            set { _points = value; }
        }
0

>>as soon as i put it in a post i make a breakthrough;)
Have that all the time. Dozens of posts I did not have to send because while posting them I found the solution!!!
As with your last code snippet, with every new Bay you declare an array of 4 new PointF structures. I do not know if that is the intention.

0

Yeah, thats the aim.
Each bay is square so all the polygons will be formed by 4 connected points.
I cant understand why its creating a reference. Its my understanding that parameters are passed by value by default.
newBay is an array of 4 PointF structures created in the main class. This is then passed to the instance of the Bay class.
Ive gone in line by line with a watch and as soon as i change a point in newBay, each instance of the Bay class has its matching point changed. Gah, why is it doing that? I ahevnt used 'ref' anywhere : /

0

Got to the bottom of it..i was passing the arrays not the values! Sjtoepit mistake! Arrays are reference type. I changed

public PointF[] Points
        {
            get { return _points; }
            set { _points = value; }
        }

to

public PointF[] Points
        {
            get { return _points; }
        }
        public void SetPoints(PointF p1, PointF p2, PointF p3, PointF p4)
        {
            _points[0] = p1;
            _points[1] = p2;
            _points[2] = p3;
            _points[3] = p4;
        }

to feed in the 4 points as values and it all works fine. A whole day wasted on a daft error. Glad i'm not the one paying my wages haha.
CHeers for the help :)

0

Glad to be of "some" assistance Ryshad. But most of the work was done by you, you may be proud of yourself!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.