2010-12-02 79 views
0

我在我的班級Class1:Panel中使用OnPaint方法。在C#圖形奇怪的旋轉

protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 
    Graphics g = e.Graphics; 
} 

旋轉並繪製矩形我使用

Matrix m = new Matrix(); 
m.RotateAt(90, rotationPoint); 
g.Transform = m; 
g.FillRectangle(Brushes.Black, rectangle) 

的問題是,轉動不工作,我希望它。

http://i52.tinypic.com/2rca2ic.png

紅場旋轉點,它的位於矩形的中間頂部。如何設置x,y和旋轉點以便旋轉會正常工作?

在90個輩分旋轉後,它應該是這樣的

i53.tinypic.com/2co25wj.png

紅色像素仍然是在同一位置。

+1

那麼,什麼*不*它看起來像旋轉後? – 2010-12-02 23:48:09

+0

像Marcelo說的......你如何設置rotationPoint? – 2010-12-02 23:57:10

回答

1

旋轉點不是您要旋轉的點。它是點,圍繞其中圖形旋轉。因此,如果您在圖形頂部繪製矩形並想旋轉它(矩形),則應將旋轉點設置爲圖形中心並將圖像旋轉90度。
這裏是例子,但這幾乎你想要什麼:

base.OnPaint(e); 

var g = e.Graphics; 
var width = g.VisibleClipBounds.Width; 
var height = g.VisibleClipBounds.Height; 
var rotationPoint = new PointF(width/2, height/2); ; 

// draw center point 
g.FillRectangle(Brushes.Red, new RectangleF(rotationPoint.X - 5, rotationPoint.Y - 5, 10, 10)); 

using (var path = new GraphicsPath()) 
{ 
    var rectangle = new RectangleF((width - 10)/2, 0, 10, 10); 
    var m = new Matrix(); 
    m.RotateAt(90, rotationPoint); 
    path.AddRectangle(rectangle); 
    path.Transform(m); 

    // draw rotated point 
    g.FillPath(Brushes.Black, path); 
}