.net 实现鼠标滑动截取picturebox相关区域高宽图片

    Dim m_ptStart
    Dim m_ptend

 


    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Try
          
              
                    Dim CurImage As Image = New Bitmap(CInt(n1.Value), CInt(n2.Value))
                    Dim g As Graphics = Graphics.FromImage(CurImage)
                    Dim FromRectangle As Rectangle
                    Dim ToRectangle As Rectangle
                    Dim DrawRectangle As New Rectangle(m_ptStart, m_ptend, CInt(n1.Value), CInt(n2.Value))
                  
                    FromRectangle = New Rectangle(m_ptStart, m_ptend, CInt(n1.Value), CInt(n2.Value))
                    ToRectangle = New Rectangle(0, 0, CInt(n1.Value), CInt(n2.Value))

                    g.Clear(Color.White)
                    '分别是:截取可能和目标图形不是一样大
                    '原有图形,目标图形的区域 <就是在你设定的图形里面的位置> ,原有图形的区域
                    g.DrawImage(PictureBox2.Image, ToRectangle, FromRectangle, GraphicsUnit.Pixel)
                    PictureBox1.Image = CurImage

          
        Catch ex As Exception
            MsgBox("发生错误,错误原因:" & ex.ToString)
        End Try

 

    End Sub

 

    Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        If rd.Checked Then
            m_ptStart = e.X
            m_ptend = e.Y
            PictureBox2.Refresh()
        End If
    End Sub

 

 

    Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        Try
            If rd.Checked Then
                If Not PictureBox2.Image Is Nothing Then
                    e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, m_ptStart, m_ptend, CInt(n1.Value), CInt(n2.Value))
                End If
            End If
        Catch ex As Exception
            MsgBox("发生错误,错误原因:" & ex.ToString)
        End Try

    End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值