2012-02-11 93 views
1

當我想用鋼筆工具如果矩形的寬度和高度小於筆寬度,則程序從如果筆對準財產得出什麼插圖繪製在C#中的矩形。但是當我設置對齊中心然後它打印一個矩形。這不是我矩形的大小。其實當時發生了什麼?Alignment屬性

例如:

 Pen p = new Pen(Color.Black, 300); 
     p.Alignment = PenAlignment.Center; 
     g.DrawRectangle(p, 100, 100,10, 10); 
     p.Dispose(); 

輸出數字是:

enter image description here

但它是如何可能得出1O像素寬度,HIGHT的矩形300像素的筆寬度?

+3

這就像用掃帚繪畫蒙娜麗莎,並問如何獲得微笑。 – 2012-02-11 17:45:20

回答

0

所以,基本上,當矩形寬度或高度小於300px,你想繪製一個黑色填充矩形?

void DrawRectangle(Graphics g, Color pencolor, int penwidth, Rectangle x) 
{ 
    if(x.Width < penwidth || x.Height < penHeight) 
    { 
     Pen p = new Pen(pencolor); 
     p.Alignment = PenAlignment.Inset; 
     g.FillRectangle(p, x); 
     p.Dispose(); 
    } 
    else 
    { 
     Pen p = new Pen(pencolor, penwidth); 
     p.Alignment = PenAlignment.Inset; 
     g.DrawRectangle(p, x); 
     p.Dispose(); 
    } 
} 

希望有所幫助。