2013-12-21 29 views
1

我在VB.NET中創建了一個小的剪切工具類程序,我可以截取任何我想要的區域,只要它是一個矩形區域即可。我選擇屏幕中的區域並將其保存爲圖像。這很容易。如何僅截取橢圓內的區域?

我的問題是,我想能夠截圖不僅矩形(標準矩形形狀的區域),但選擇/繪製橢圓和屏幕截圖的它的內部部件。見下圖:

screenshot only the inside of the Ellipse. the rest can be transparent

有沒有辦法實現這個或者我可以使用任何圖書館嗎?

這裏是我當前的代碼:

Public Class Form3 
    Private _bRubberBandingOn As Boolean = False 
    Private _pClickStart As New Point 
    Private _pClickStop As New Point 
    Private _pNow As New Point 

    Private Sub Form3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown 
     Me._bRubberBandingOn = Not _bRubberBandingOn 

     If Me._bRubberBandingOn Then 
      If _pClickStart = Nothing Then _pClickStart = New Point 
      _pClickStart.X = e.X 
      _pClickStart.Y = e.Y 
      _pNow.X = e.X 
      _pNow.Y = e.Y 
     End If 
     Me.Invalidate() 
    End Sub 

    Private Sub Form3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 

     If Me._bRubberBandingOn Then 
      If _pNow = Nothing Then _pNow = New Point 
      Me._pNow.X = e.X 
      Me._pNow.Y = e.Y 
      Me.Invalidate() 
     End If 
    End Sub 

    Private Sub Form3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp 

     Me._bRubberBandingOn = Not Me._bRubberBandingOn 
     If Not Me._bRubberBandingOn Then 
      If _pClickStop = Nothing Then _pClickStop = New Point 
      _pClickStop.X = e.X 
      _pClickStop.Y = e.Y 
      Me.Invalidate() 

     End If 
    End Sub 

    Private Sub Form3_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 
     Dim _rRectangle As New Rectangle 
     Dim _penNew As New Pen(Color.Black, 2) 

     _rRectangle.X = _pClickStart.X 
     _rRectangle.Y = _pClickStart.Y 

     If Me._bRubberBandingOn Then 
      _rRectangle.Width = Me._pNow.X - _pClickStart.X 
      _rRectangle.Height = Me._pNow.Y - _pClickStart.Y 
     Else 
      _rRectangle.Width = Me._pClickStop.X - _pClickStart.X 
      _rRectangle.Height = Me._pClickStop.Y - _pClickStart.Y 

     End If 
     _penNew.DashStyle = Drawing2D.DashStyle.Solid 
     e.Graphics.DrawEllipse(_penNew, _rRectangle) 

    End Sub 
End Class 

有沒有辦法實現這個或者我可以使用任何圖書館嗎? 這是任何方式來獲取白線/形狀的手柄,然後用它來創建一個屏幕截圖?我真的搜索了這個,但沒有發現任何有意義的東西。 提前感謝您的時間。

回答

0

拿你繪製的橢圓上頂部的圖像並執行以下操作:

Dim theBitmap As Bitmap = DirectCast(Image.FromFile("PathToFileYouAreDrawingEllipseOn.bmp"), Bitmap) 
Dim theEllipseBitmap As New Bitmap(theBitmap.Width, theBitmap.Height) 
Dim theGraphics As Graphics = Graphics.FromImage(theEllipseBitmap) 
Dim theGraphicsPath As New GraphicsPath() 

' The (10,10) coordinates here are made up, you will need to take what is drawn by the user (starting x,y; ending x,y, etc.) 
theGraphicsPath.AddEllipse(10, 10, theBitmap.Width - 20, theBitmap.Height - 20) 
theGraphics.Clear(Color.Magenta) 
theGraphics.SetClip(theGraphicsPath) 
theGraphics.DrawImage(theBitmap, New Rectangle(0, 0, theBitmap.Width, theBitmap.Height), 0, 0, theBitmap.Width, theBitmap.Height, _ 
GraphicsUnit.Pixel) 
theGraphics.Dispose() 
theEllipseBitmap.MakeTransparent(Color.Magenta) 

' Save the ellipse bitmap to a PNG file format 
string fileName = "PathToYourDesiredOutput.png" 
theEllipseBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png)