请教高手,如何在picturebox里绘图

safematch 2005-08-13 02:24:22
picturebox里已经有一幅图片,想在上边画一个矩形,框取矩形里的图像,应该怎么画,请高手指教!
...全文
515 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
dolfen 2006-03-15
  • 打赏
  • 举报
回复
mark
safematch 2005-09-02
  • 打赏
  • 举报
回复
真是太感动了!T_T
很久没见过像dbspro(冷锋)哥哥这样古道热肠的好人了。在这里向您表示十二万分的敬意!并强烈要求斑竹发出全体csdn会员向dbspro(冷锋)同志学习的号召。
冷锋 2005-09-01
  • 打赏
  • 举报
回复
嘿,帮忙就帮到底,好人做到底吧!

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
zeusvenus 2005-09-01
  • 打赏
  • 举报
回复
主要是结合鼠标坐标做Rectangle局部重绘。
safematch 2005-09-01
  • 打赏
  • 举报
回复
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)

bit.Save("d:\photo\111.bmp")

'ClosePreviewWindow()

'Me.Close()

End Sub
这样能截取出矩形内部的图像吗?请大家帮忙,万分感谢!
冷锋 2005-08-30
  • 打赏
  • 举报
回复
嘿嘿,为了100分只好辛苦点了:


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

End Class
safematch 2005-08-30
  • 打赏
  • 举报
回复
感谢楼上的dbspro(冷锋)哥哥细心的帮助,现在还有三个问题,一是按照你的代码画完矩形后鼠标抬起矩形就消失了,怎么让他留在picturebox上呢,二是我的本意不是想用鼠标拖出一个矩形,而是原本就画一个固定大小的矩形,用鼠标拖动来移动它的位置,三是我的最终目的是想将矩形框中的图像从原图像中截取出来,这部分应该怎么实现一一点头绪都没有,请好心的高手们帮忙啊
冷锋 2005-08-30
  • 打赏
  • 举报
回复
楼主是初学者,要求的代码是拿来就可以用的,下面是我即时写的,你看看是不是你想要的结果!

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
safematch 2005-08-29
  • 打赏
  • 举报
回复
楼上的哥哥这样的代码不是我希望的功能啊,我是想从现成的图片中裁出一个矩形,可以用鼠标移动来选取
3tzjq 2005-08-13
  • 打赏
  • 举报
回复
打错字:Read >> Red
3tzjq 2005-08-13
  • 打赏
  • 举报
回复
优化一下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
caojinrong 2005-08-13
  • 打赏
  • 举报
回复
Dim g As Graphics = Graphics.FromImage(PictureBox.Image)
g.DrawRectangle(New Pen(Color.Black), New Rectangle(x, y, Width, Height))
hamadou 2005-08-13
  • 打赏
  • 举报
回复
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
safematch 2005-08-13
  • 打赏
  • 举报
回复
呵呵,那就多说几句呗
yizhixiaozhu 2005-08-13
  • 打赏
  • 举报
回复
这个一句两句说不完的 呵呵
safematch 2005-08-13
  • 打赏
  • 举报
回复
楼上的哥哥能不能说的具体点啊,小弟初次接触,请高手哥哥们说的详细点吧
冷锋 2005-08-13
  • 打赏
  • 举报
回复
在图片框控件的Paint事件中有一个e参数,这个参数中有一个Graphics属性,用它画图就可以了!
sz_lgp 2005-08-13
  • 打赏
  • 举报
回复
定义矩形,定义一个BITMAP=PICTUREBOX.IMAGE,bitmap有裁剪、复制、扭曲等操作。具体做法,你自己搞定。
safematch 2005-08-13
  • 打赏
  • 举报
回复
还需要用鼠标来移动这个矩形来选取需要的图像,应该怎么写呢

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧