2012-03-26 242 views
0

我正在使用DrawImage調整圖像大小。動態繪製圖像時設置x和y座標

Graphics.DrawImage(SourceImage,0,0,200,200); 

這裏的源圖像與共同座標開始0
想我需要計算的X和Y座標動態怎麼辦我去嗎?
默認情況下,圖像應該從位置20(即; x)和20(即y)開始。
如果我調整窗體大小,它應該根據調整大小的圖像按比例計算,也就是說,如果默認情況下它是20比窗體大小調整多少?

謝謝

+0

你能澄清你的問題嗎? – daryal 2012-03-26 13:46:38

+0

我需要調整圖像大小,使圖像以指定的位置開始。 – Guddu 2012-03-26 13:48:00

+0

你的意思是說,雖然調整圖像變化的起點? – daryal 2012-03-26 13:49:31

回答

0

您可以註冊到窗體的ResizeEnd事件,並可以重新繪製圖像。就像是;

public Form1() 
    { 
     InitializeComponent(); 

     this.ResizeEnd += new EventHandler(Form1_ResizeEnd); 

    } 

    void Form1_ResizeEnd(object sender, EventArgs e) 
    { 
     //draw the image again using the related calculation 
    } 
0

從您的問題,不清楚表格的大小如何與所需的座標相關。

表單有一個ClientRectangle屬性,您可以使用它來計算座標。如果你想在例如右下角顯示的圖像,你會:

protected override void OnPaint(PaintEventArgs e) 
{ 
    int x = this.ClientRectangle.Width - 200; 
    int y = this.ClientRectangle.Height - 200; 
    e.Graphics.DrawImage(SourceImage, x, y, 200, 200); 
} 

我假設DrawImage代碼在Paint事件處理程序,然後你可以要麼

  • 使用SetStyle(ControlStyles.ResizeRedraw, true);在窗體的構造,所以Paint當窗體大小
  • 添加事件處理程序Resize事件並調用被稱爲Invalidate();自己