不使用DrawLine方法绘制线

时间:2015-04-02 21:50:22

标签: c# winforms draw dda

这是我的代码,我想在不使用c#中的drawLine方法的情况下实现DDA算法。我尝试使用PutPixel方法,但它没有用。我的窗户里什么都没有。有没有办法在c#中使用drawLine方法绘制线条?

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        grafik = e.Graphics;

        DDACiz(ilk.X, ilk.Y, ikinci.X, ikinci.Y, grafik, Color.DarkRed);


    }

    void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
    {
        System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
        bm.SetPixel(0, 0, Color.DarkRed);
        g.DrawImageUnscaled(bm, x, y);
    }

    void DDACiz(int x1, int y1, int x2, int y2,Graphics grafik, Color renk)
    {

        int PikselSayisi;

        int dx, dy;
        float x, xFark;
        float y, yFark;

        dx = x2 - x1;
        dy = y2 - y1;

        PikselSayisi = Math.Abs(dx) > Math.Abs(dy) ? Math.Abs(dx) : Math.Abs(dy);

        xFark = (float)dx / (float)PikselSayisi;
        yFark = (float)dy / (float)PikselSayisi;

        x = (float)x1;
        y = (float)y1;

        while (PikselSayisi!=0)
        {
           PutPixel(grafik,(int)Math.Floor(x + 0.5F),(int) Math.Floor(y + 0.5f),renk);
            x += xFark;
            y += yFark;
            PikselSayisi--;
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

没有Graphics.DrawPoint方法,因此要使用Graphics对象绘制单个像素,您需要使用Graphics.FillRectangle

更改

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
    bm.SetPixel(0, 0, Color.DarkRed);
    g.DrawImageUnscaled(bm, x, y);
}

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    g.FillRectangle(Brushes.DarkRed, x, y, 1, 1);
}

或者如果您想使用Color c:

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    using (SolidBrush brush = new SolidBrush(c) )
       g.FillRectangle(brush , x, y, 1, 1);
}

您还可以使用绘制位图的方法,但需要将其设置为1x1像素宽,或确保它是透明的,并使用CompositingMode.SourceOver

写一个线条绘制方法是一个有趣的练习; PenWidths以外1.0比{{1}}更难看,更不用说完全不透明的alpha通道了。

相关问题