2015-04-02 99 views
0

这里是我的代码,我想实现DDA算法,而不使用c#中的drawLine方法。我试图使用PutPixel方法,但它不起作用。我的窗户没有任何东西。有没有什么办法可以在c#中使用drawLine方法画线?不使用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--; 
     } 
    } 
} 
} 
+0

'我尝试使用PutPixel方法,但它不工作,如果你的问题出现在这个方法中,删除从你的问题不必要的代码。 http://sscce.org/ – EZI 2015-04-02 21:57:12

+2

要使用Graphics对象绘制像素,您需要使用'FillRectangle(yourBrush,x,y,1,1)' – TaW 2015-04-02 21:57:14

回答

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); 
} 

您也可以使用自己绘制的位图的appoach,但你需要使它广泛要么1x1像素,或者确保它是透明的,还可以使用CompositingMode.SourceOver

写一个线条绘制方法是一个有趣的练习;比看起来更困难,PenWidths1.0之外更难,更不用说除了完全不透明以外的其他alpha通道。

+0

所有这些都可以使用! – 2015-04-02 22:27:36