2011-11-18 66 views
4

我已經試過這樣:如何使用Graphics對象以特定的程度旋轉RectangleF?

g.RotateTransform(degrees); 

但沒有happens.I有一個圖形對象和使用這種方法一個矩形對象女巫IM繪圖:

g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle); 

,我需要以某種方式旋轉矩形再次繪製它。

請與代碼示例請與簡單的解釋。

編輯:這是我使用的實際代碼:

 public void Draw(Graphics g,PointF location,Color clearColor) 
    { 
     rectangle.Location = location; 
     g.Clear(clearColor); 
     g.RotateTransform(10); 
     //g.FillRectangle(new SolidBrush(Color), rectangle); 
     g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle); 
    } 

每一幀我把這種功能,我使用窗體的Paint事件的e.Graphics對象的圖形和我有一個timer女巫只調用this.Refresh();

編輯2: OK我打一點與轉換和g.RotateTransform旋轉graphycs整個cordinate系統對象,我需要在不改變cordinate系統

0123僅旋轉矩形
+0

你是按這個順序調用兩條線嗎?你能否顯示整個代碼片段(包括諸如實際度數之類的東西) –

+2

沒有TranslateTransform,那很麻煩。 http://www.bobpowell.net/transformations.htm –

+0

@HansPassant - 這很神祕......照顧解釋? –

回答

1

您可以嘗試使用矩陣與RotateAt方法來圍繞着矩形旋轉:

using (Matrix m = new Matrix()) { 
    m.RotateAt(10, new PointF(rectangle.Left + (rectangle.Width/2), 
          rectangle.Top + (rectangle.Height/2))); 
    g.Transform = m; 
    using (TextureBrush tb = new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")) 
    g.FillRectangle(tb, rectangle); 
    g.ResetTransform(); 
} 

ResetTransform()會後打開圖形恢復正常處理。

+0

當我使用ResetTransform()時,旋轉也不會恢復到正常狀態嗎? – Bosak

+0

@Bosak不適用於被調用的一個FillRectangle。這段代碼基本上說,開始繪製一個旋轉,繪製東西,然後停止繪製旋轉。希望有所幫助。 – LarsTech