Question

Can DrawLine handle coordinates outside the defined area?

For example myGraphics.DrawLine(MyPen, -20, -80, 20, 90);

I would expect this to produce a line correctly as though it had used an infinite canvas but plotting only the section within my graphic.

My code is as follows. I am plotting movement from coordinates recorded in a database. Occasionally the subject moves further than expected, beyond the edges of my bitmap. I do not check for this occurrence as I was relying on DrawLine to handle it.

Bitmap Border = new Bitmap(5000, 5000);
Border.SetResolution(254, 254);

Graphics MyGraphics= Graphics.FromImage(Border);
Pen MyPen = new Pen(Color.Black, 1);


for (Int32 Point = 1; Point <= Points; Point++)
{
   XCoord2 = XCoord1;
   YCoord2 = YCoord1;
   XCoord1 = *READ FROM DATABASE*
   YCoord1 = *READ FROM DATABASE*

   if (Point > 1)
   {
        MyGraphics.DrawLine(MyPen, XCoord1, YCoord1, XCoord2, YCoord2);
   }
}

In reality, my plots work most of the time. However I do get an occasional graphic with missing lines or with an obscure line originating from a strange coordinate.

In summary, should the behaviour of DrawLine predictable with unusual parameters. Should I introduce some trigonometry to force the plots to always be within my grid?

Était-ce utile?

La solution 2

After more experimentation, I finally cured my problem with...

SolidBrush WhiteBrush = new SolidBrush(Color.White);
myGraphics.FillRectangle(WhiteBrush,0,0,5000,5000);

i.e. I gave my graphics a solid white background before I drew any lines. Before I was drawing a black line on a NULL background. I have no idea why this would affect anything, but it did!

Autres conseils

The actual limits are a billion positive or negative see this past question (that used .net )

What are the hard bounds for drawing coordinates in GDI+?

My guess is that your database pulls are wrong, this can happen if you are using a string data storage and forcing that to be parsed.

Add a thread.sleep() and have it Debug.WriteLine the new pulls (or just breakpoint things), likely a value is getting in there that is either odd or getting parsed oddly

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top