Public Class Form1 : Inherits System.Windows.Forms.Form
Private M_Point As Point
Private M_DrawFlag As Boolean
Private M_Rect As Rectangle
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.White
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(32, 39)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(472, 361)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(120, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(144, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(588, 421)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
M_Rect = New Rectangle(0, 0, 100, 100)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If M_Rect.Contains(e.X, e.Y) Then
M_DrawFlag = True
M_Point = New Point(e.X, e.Y)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If M_DrawFlag = True Then
M_Rect = New Rectangle(M_Rect.X + e.X - M_Point.X, M_Rect.Y + e.Y - M_Point.Y, M_Rect.Width, M_Rect.Height)
M_Point = New Point(e.X, e.Y)
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
M_DrawFlag = False
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawRectangle(New Pen(Color.Black), M_Rect)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim S_Image As New Bitmap(M_Rect.Width, M_Rect.Height)
Dim S_Graphics As Graphics = Graphics.FromImage(S_Image)
S_Graphics.DrawImage(PictureBox1.Image, 0, 0, New RectangleF(M_Rect.X, M_Rect.Y, M_Rect.Width, M_Rect.Height), GraphicsUnit.Pixel)
Dim S_SaveDialog As New SaveFileDialog
With S_SaveDialog
.Filter = "BMP Files(*.BMP)|*.BMP|JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF"
.RestoreDirectory = False
.FilterIndex = 0
End With
If S_SaveDialog.ShowDialog = DialogResult.Cancel Then
Return
End If
S_Image.Save(S_SaveDialog.FileName)
End Sub
End Class
dbspro(冷锋)哥哥,谢谢您又一次帮助了我,您真是古道热肠啊,不过还有一个问题您在程序里没有提到,就是怎样将最后形成的矩形框内部的图像被裁剪出来。我自己这样试过但没有成功,不知道错在哪里,请您指正,也请其他高手帮忙!
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim bit As Bitmap = picSave.Image
Dim myGraphics As Graphics = Graphics.FromImage(picSave.Image)
Dim sourceRectangle As New Rectangle(M_LTPoint.X, M_LTPoint.Y, 150, 180)
Dim targetRectangle As New Rectangle(M_LTPoint.X, M_LTPoint.X, 150, 180)
myGraphics.DrawImage(bit, targetRectangle, sourceRectangle, GraphicsUnit.Pixel)
Public Class Form1 : Inherits System.Windows.Forms.Form
Private M_Point As Point
Private M_DrawFlag As Boolean
Private M_Rect As Rectangle
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.White
Me.PictureBox1.Location = New System.Drawing.Point(47, 39)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(469, 337)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(588, 421)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
M_Rect = New Rectangle(0, 0, 100, 100)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If M_Rect.Contains(e.X, e.Y) Then
M_DrawFlag = True
M_Point = New Point(e.X, e.Y)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If M_DrawFlag = True Then
M_Rect = New Rectangle(M_Rect.X + e.X - M_Point.X, M_Rect.Y + e.Y - M_Point.Y, M_Rect.Width, M_Rect.Height)
M_Point = New Point(e.X, e.Y)
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
M_DrawFlag = False
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawRectangle(New Pen(Color.Black), M_Rect)
End Sub
Public Class Form1 : Inherits System.Windows.Forms.Form
Private M_LTPoint As Point
Private M_RBPoint As Point
Private M_DrawFlag As Boolean
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.White
Me.PictureBox1.Location = New System.Drawing.Point(61, 45)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(371, 276)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(572, 365)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
M_DrawFlag = True
M_LTPoint = New Point(e.X, e.Y)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
M_RBPoint = New Point(e.X, e.Y)
Me.PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
M_DrawFlag = False
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If M_DrawFlag = False Then
Return
End If
e.Graphics.DrawRectangle(New Pen(Color.Black), M_LTPoint.X, M_LTPoint.Y, M_RBPoint.X - M_LTPoint.X, M_RBPoint.Y - M_LTPoint.Y)
End Sub
End Class
优化一下hamadou(闵峰)版主的代码:
private p As Pen
构造函数里:
p = new Pen(Color.Read,1)'1表示线宽
p.DashStyle = DashStyle.Solid'实线,可改为其它线型
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'Paint会在每次Refresh时调用,那么频繁的取图效益不太理想
'Dim image As Image = image.FromFile("f:\1.jpg")
if(PictureBox1.Image is nothing) PictureBox1.Image = Image.FromFile("f:\1.jpg")
'Dim g As Graphics = e.Graphics
'g.DrawImage(image, New Rectangle(0, 0, image.Width, image.Height))
e.Graphics.DrawRectangle(p, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)
'reg = New Region(New Rectangle(0, 0, image.Width, image.Height))
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If reg Is Nothing Then Exit Sub
If reg.IsVisible(e.X, e.Y) Then
Me.TextBox1.Text = e.X.ToString
Me.TextBox2.Text = e.Y.ToString
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim image As Image = image.FromFile("f:\1.jpg")
Dim g As Graphics = e.Graphics
g.DrawImage(image, New Rectangle(0, 0, image.Width, image.Height))
g.DrawRectangle(Pens.Red, New Rectangle(0, 0, image.Width, image.Height))
reg = New Region(New Rectangle(0, 0, image.Width, image.Height))
End Sub