【问题标题】:How to screenshot only the area inside an Ellipse?如何仅截取椭圆内的区域?
【发布时间】:2013-12-21 12:00:40
【问题描述】:

我正在 VB.NET 中创建一个类似截图工具的小程序,我可以截取任何我想要的区域,只要它是一个矩形区域。我选择屏幕中的区域并将其保存为图像。这很容易。

我的问题是,我不仅希望能够截取矩形(标准矩形形状区域),还希望能够选择/绘制椭圆并截取其内部。见下图:

有什么方法可以实现这一点或我可以使用任何库吗?

这是我当前的代码:

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

有什么方法可以实现这一点或我可以使用任何库吗? 有什么方法可以处理那个画线/形状,然后用它来创建屏幕截图?我实际上对此进行了搜索,但还没有发现任何有意义的东西。 提前感谢您的宝贵时间。

【问题讨论】:

    标签: vb.net graphics screenshot


    【解决方案1】:

    获取要在其上绘制椭圆的图像并执行以下操作:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 2012-04-25
      • 2014-09-07
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多