Graphics.SetClip 方法

摘要:Graphics.SetClip 方法

今天看討論區的這篇文章請問如何把DrawImageUnscaled畫出來的東西放到剪貼簿呢?,看到提問者用了SetClip的方法,之前沒用過,於是乎到MSDN查詢了一下,將用法整理如下,大家可以參考看看,程式碼部分是直接用MSDN上面的範例

SetClip的作用是限定繪圖的區域,可以參考一下下面網頁
http://msdn2.microsoft.com/zh-tw/library/ms142116(VS.80).aspx
依照範例程式碼建立新的Windows Form應用程式之後,貼上下面程式碼來測試



    Public Sub SetClipGraphicsCombine(ByVal e As PaintEventArgs)
        ' Create temporary graphics object and set its clipping region.
        Dim newGraphics As Graphics = Me.CreateGraphics()
        newGraphics.SetClip(New Rectangle(0, 0, 100, 100))
        ' Update clipping region of graphics to clipping region of new graphics.
        e.Graphics.SetClip(newGraphics, Drawing2D.CombineMode.Replace)
        ' Fill rectangle to demonstrate clip region.
        e.Graphics.FillRectangle(New SolidBrush(Color.Black), 0, 0, 500, 300)
        ' Release new graphics.
        newGraphics.Dispose()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        SetClipGraphicsCombine(e)
    End Sub
End Class
就可以發現呼叫FillRectangle時是指定畫到500,300這麼大,但是實際跑出來的結果只有到100,100