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
本文介绍了一个使用C#实现的图片截取与绘制功能的示例代码。该示例展示了如何通过PictureBox控件响应鼠标点击和移动事件来定义截取区域,并将截取的图片显示到另一个PictureBox中。

被折叠的 条评论
为什么被折叠?



