2012-01-31 159 views
0

我有繪圖和編輯矢量圖形的WinForms旋轉矢量圖形

我有圖片,矩形,橢圓區域等,我知道如何通過鼠標移動來調整他們的應用程序。但我不知道如何通過鼠標移動來旋轉它們。

我把對象繪製成圖形。

我試過這個,但它沒有工作。

g.TranslateTransform((float)(this.Rectangle.X + this.Rectangle.Width/2), (float)(this.Rectangle.Y + this.Rectangle.Height/2)); 
g.RotateTransform(this.Rotation); 
g.TranslateTransform(-(float)(this.Rectangle.X + this.Rectangle.Width/2), -(float)(this.Rectangle.Y + this.Rectangle.Height/2)); 
//g.TranslateTransform(-(float)(rect.X + rect.Width/2), -(float)(rect.Y + rect.Height/2)); 

g.DrawImage(img, rect); 

g.ResetTransform(); 

這沒有工作,因爲我不知道如何找到新的(旋轉)位置對象的角落,所以我不能夠調整的是...

+0

請註明什麼框架你使用(的WinForms),同樣在標籤 – 2012-01-31 14:43:13

+0

我不知道在哪裏的角落,我無法改變它,即使我知道他們是誰。因爲尺寸是在「不旋轉」的軸...如果你明白:-) – MadMaxx 2012-01-31 15:52:37

+0

你應該發佈更多的代碼。 'this'指向什麼?你的對象如何被操縱(被拐角拖拽)?基本上,你想做什麼? – 2012-01-31 16:15:06

回答

1

您需要申請高中三角學。有很多文章,如果你谷歌「graphics.drawimage旋轉」

但是,首先,你不應該轉換圖形對象本身。你只是想找到你的圖像的新邊界框。要做到這一點:

  1. 以圖像的原點爲中心的邊界框。記住這被定義爲三點的DrawImage的利益(圖片,點[])

    Point[] boundingBox = { new Point(-width /2, -height/2), 
             new Point(width/2, -height/2), 
             new Point(-width/2, height/2) }; 
    
  2. 使用觸發使其旋轉。通過下面的函數Feed中的每個點:

    Point rotatePointAroundOrigin(Point point, float angleInDegrees) { 
        float angle = angleInDegrees * Math.PI/180; // get angle in radians 
        return new Point(point.X * Math.Cos(angle) - point.Y * Math.Sin(angle), 
             point.X * Math.Sin(angle) + point.Y * Math.Cos(angle)); 
    } 
    
  3. 翻譯boundind框,它已去。爲每個點添加寬度/ 2和高度/ 2,再加上任何額外的數量。

  4. 呼叫DrawImage(image, boundingBox)

+0

謝謝,我試過了這,但是當我嘗試繪製DrawImage(圖像,點[])它會引發NotImplementedException ... – MadMaxx 2012-02-09 19:23:45

+0

使用Google搜索「DrawImage NotImplementedException」產生http://blog.felipel.com/2009/06/notimplementedexception-in-graphics- drawimage /顯然,DrawImage是一個奇怪的行爲,它不接受所有的Image類。 – 2012-02-12 19:13:33

+0

看起來我也在旋轉點的公式中犯了一個錯誤。如果結果點關閉並且不形成矩形或平行四邊形,這也會導致NotImplementedException。 – 2012-02-12 19:23:14