.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

你可能感兴趣的:(.net,exception,object,image,图形,n2)