简体   繁体   中英

I'm trying to draw something with float value in c# but I'm not successful

I want to draw some sort graphics on winform using following code :

private void button1_Click(object sender, EventArgs e)
        {
            Pen myPen = new System.Drawing.Pen(Color.Red);
            Graphics handler = this.CreateGraphics();


            double[] nextPt = new double[2];
            double[] maurerPt = new double[2];
            for (int index = 1; index < 361; index++)
            {
                nextPt = calcRose(index * d);

                //this line is incorrect but Drawline only accept Point which is int
                //and valued are in fractions thats why I use double
                handler.DrawLine(maurerPt[0], maurerPt[1], nextPt[0], nextPt[1];

                nextPt.CopyTo(maurerPt,2);
            }

            myPen.Dispose();
            handler.Dispose();

        }

the problem is I want to create some sort of mathematic graph something called "maruer rose" and since my lines value are in fractions so I can't use Point which it only accept int values

how can I feed drawline with double or float value?

regards.

Hans should have posted his comment as an answer, but he is correct on both accounts. DrawLine() has an overload that accepts a PointF , so that's not a problem.

Your next issue will be figuring out why your graph seems to disappear randomly. You should do all of your drawing in a Control's Paint event or, if in a derived class, by overriding OnPaint() , always drawing to the supplied Graphics object ( e.Graphics ).

There will be times that your control repaints itself, perhaps because it was minimized, its dimensions were changed, whatever. Your code only paints when a button is clicked, which means that a system forced re-paint will wipe out your graph.

Next, your code has a potential memory "leak". What happens if anything between creating the pen/Graphics objects and calling Dispose() on them? In this specific case your program would likely just crash, but in general and more importantly, your calls to Dispose() will not occur and you have temporarily "leaked" some native resources.

You should be wrapping the creation of types that implement IDisposable in using statements, ie

using( var myPen = new System.Drawing.Pen(Color.Red) )
using( var handler = this.CreateGraphics() )
{
    // use 'myPen' and 'handler' here
}

This construct is actually a try/finally block, so even if something inside the curlies throws an exception Dispose() will still be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM