I am creating a document based application and i want to draw a horizontal line underlying the text. But, line should not be straight. i want to draw a line like this.
Zigzag line

Currently i am using System.Graphics object to draw any object.

private void DrawLine(Graphics g, Point Location, int iWidth)
{
    iWidth = Convert.ToInt16(iWidth / 2);
    iWidth = iWidth * 2;
    Point[] pArray = new Point[Convert.ToInt16(iWidth / 2)];
    int iNag = 2;
    for (int i = 0; i < iWidth; i+=2)
    {
        pArray[(i / 2)] = new Point(Location.X + i , Location.Y + iNag);
        if (iNag == 0)
            iNag = 2;
        else
            iNag = 0;
    }
    g.DrawLines(Pens.Black, pArray);
}

UPDATED:
Above code is working fine and line draws perfectly but, this code effects on application performance. Is there another way to do this thing.

有帮助吗?

解决方案

If you want fast drawing just make a png image of the line you want, with width larger than you need and then draw the image:

private void DrawLine(Graphics g, Point Location, int iWidth)
{
    Rectangle srcRect = new Rectangle(0, 0, iWidth, zigzagLine.Height);
    Rectangle dstRect = new Rectangle(Location.X, Location.Y, iWidth, zigzagLine.Height);

    g.DrawImage(zigzagLine, dstRect, srcRect, GraphicsUnit.Pixel);
}

zigzagLine is the bitmap.

valter

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top