2011-08-23 4 views
1

私はある点に四角形を中心に配置しようとしており、それを行うために必要な数学を理解することができません。ズームインしながら与えられた点にキャンバスを中心に置く

これで使用されているコンテキストは、ビットマップを描画し、ユーザが指定したポイントにズームインすることを可能にする形態、及びパン/スクロールである。

ここに私のコードは、のために現在働くですClientRectangleの真ん中にCanvasBoundsを中心に:

private void UpdateCanvas() 
    { 
     int canvasWidth = (int)(_bitmap.Width * _zoomRatio); 
     int canvasHeight = (int)(_bitmap.Height * _zoomRatio); 

     Point canvasLocation = new Point((ClientRectangle.Width - canvasWidth)/2, (ClientRectangle.Height - canvasHeight)/2); 

     CanvasBounds = new Rectangle(canvasLocation, new Size(canvasWidth, canvasHeight)); 
    } 

_zoomRatio

は、キャンバスのサイズを調整ズームです。 1.0は100%、2.0は200%などとなります。

基本的には、この関数にマウス入力によるポイントを与え、そのポイントをcanvasBounds矩形の中心として使用したいと考えています。ユーザーが水平スクロールバーと垂直スクロールバーを操作すると、_centerPointを変更してCanvasBoundsを更新できます。

答えて

0

私はあなたは自分のキャンバスサイズを使用して「ポイント」をオフセットする必要があると思う:

private void UpdateCanvas(Point mousePoint) 
{ 
    int canvasWidth = (int)(_bitmap.Width * _zoomRatio); 
    int canvasHeight = (int)(_bitmap.Height * _zoomRatio); 
    int canvasX = (mousePoint.X - (canvasWidth/2)); 
    int canvasY = (mousePoint.Y - (canvasHeight/2)); 
    CanvasBounds = new Rectangle(canvasX, canvasY, canvasWidth, canvasHeight); 
} 

それはあなたが探しているものではありません場合は、多分、単純なスクリーンショットで自分の投稿を編集します。

関連する問題