讨论一个关于region的问题!

hamadou 2005-02-28 05:44:18
讨论一个关于region的问题。这几天常看到有人提类似这样的问题。就是对于picturebox中一幅图片如何取得它的不同区域(可以是规则的或者不规则的)
假设是这样的图形:(注:----表示一种颜色,假如就是红色的区域,0000部分表示绿色区域,****部分表示兰色区域)
----0000****
----0000****
----0000****
那么,如果图片的大小不确定,三个区域的大小不确定。如何取得不同区域后,当鼠标进入某个区域然后在该区域的背景出现一个边框,(或者其他效果,来表示鼠标进入了这个区域)
如何实现呢???
希望大家帮忙!
我看了一些类似的文章并且做了一些测试。用了如下的方法逐行扫描图片的像素来取得区域,然后在这个区域使用画边框的方法。但效果显示不出来。希望大家不要被我的思路所累。给出自己的见解。谢谢!
Public Function myRegionConvert(ByVal bitmap As Bitmap, ByVal transparencyKey As Color)
'If (bitmap is nothing ) Then throw new ArgumentNullException( "Bitmap", "Bitmap cannot be null!" )
If bitmap Is Nothing Then Exit Function
Dim yMax As Integer = bitmap.Height
Dim xMax As Integer = bitmap.Width
Dim region As Region = New Region(New Rectangle(0, 0, xMax, yMax)) '一个整个图片的区域
Dim path As GraphicsPath = New GraphicsPath()
Dim x As Integer, y As Integer

Try
For y = 0 To yMax - 1
For x = 0 To xMax - 1
'象素点的颜色与指定颜色不同
'MessageBox.Show(CStr(x) + Chr(13) + CStr(y))
If Not (bitmap.GetPixel(x, y).Equals(transparencyKey)) Then
'获取这个小的点区域
Dim rect As Rectangle = New Rectangle(x, y, 1, 1)
path.AddRectangle(rect)
End If
Next
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

'取得与其他部分不相交的部分
region.Exclude(path)
Return region
End Function
...全文
185 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
T2 2005-03-02
  • 打赏
  • 举报
回复
Dim pens As New Pen(Color.Gold)
Dim p As GraphicsPath
Public Function myRegionConvert(ByVal bitmap As Bitmap, ByVal transparencyKey As Color)
'If (bitmap is nothing ) Then throw new ArgumentNullException( "Bitmap", "Bitmap cannot be null!" )
If bitmap Is Nothing Then Exit Function
Dim yMax As Integer = bitmap.Height
Dim xMax As Integer = bitmap.Width
Dim region As Region = New Region(New Rectangle(0, 0, xMax, yMax)) '一个整个图片的区域
Dim path As GraphicsPath = New GraphicsPath
Dim x As Integer, y As Integer

Try
For y = 0 To yMax - 1
For x = 0 To xMax - 1
'象素点的颜色与指定颜色不同
'MessageBox.Show(CStr(x) + Chr(13) + CStr(y))
If (bitmap.GetPixel(x, y).A = transparencyKey.A And bitmap.GetPixel(x, y).B = transparencyKey.B And bitmap.GetPixel(x, y).G = transparencyKey.G And bitmap.GetPixel(x, y).R = transparencyKey.R) Then
'获取这个小的点区域
Dim rect As Rectangle = New Rectangle(x, y, 1, 1)
path.AddRectangle(rect)
End If
Next
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

'取得与其他部分不相交的部分
'region.Exclude(path)
Return path
End Function

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim bm As Bitmap
bm = Me.PictureBox1.Image
p = myRegionConvert(Me.PictureBox1.Image, bm.GetPixel(e.X, e.Y))
Me.PictureBox1.Refresh()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If Not p Is Nothing Then
e.Graphics.DrawPath(pens, p)
End If
End Sub
速度慢
xiaohutushen 2005-03-01
  • 打赏
  • 举报
回复
帮你up
amendajing 2005-03-01
  • 打赏
  • 举报
回复
mark
hamadou 2005-03-01
  • 打赏
  • 举报
回复
我想做个用户控件(继承自picturebox),我给它的image指定一张图片后,我想在onpaint里面进行扫描,把不同颜色的区域路径得到。当鼠标移动到不同的区域时,在这个区域显示一个提示。这个提示是显示背景也好,显示边框也好。 前几天看到这个lyj670(空中一只鸟)的兄弟问过类似的问题,也想知道答案。
希望大家给些帮助。谢谢!
lyj670 2005-03-01
  • 打赏
  • 举报
回复
up
T2 2005-03-01
  • 打赏
  • 举报
回复
可能我没理解你的意思,当鼠标移到哪个Picturebox则引发Move事件,为什么要得到路径?
hamadou 2005-03-01
  • 打赏
  • 举报
回复
做出来这样任意形状的picturebox的控件是吧?呵呵!
我如何得到不规则的图形的路径呢?这个难道不是解决我提出问题的关键吗?如果路径得到了,我就可以得到这个区域了。得到区域了,就可以显示了。可以显示了,就不用再弄什么自定义的picturebox控件了。呵呵!
T2 2005-03-01
  • 打赏
  • 举报
回复
比如你的图片中有红色,蓝色,绿色三中颜色,分布三个不同的区域,按区域把整张图切割成三个图,这三个图是不规则的。然后把这三个图放到三个PictureBox中,再定义PictureBox的形状,如:
Dim oPath As New GraphicsPath
'绘制GraphicsPath
oPath.AddArc(...)
oPath.AddLine(...)
.....
Me.PictureBox1.Region = New Region(oPath)
要保证PictureBox的形状与图片一致,这个比较麻烦
做出PictureBox就没问题了
hamadou 2005-03-01
  • 打赏
  • 举报
回复
楼上的朋友说的详细点好吗?
T2 2005-03-01
  • 打赏
  • 举报
回复
这方法不好,有一个办法是将整张图按颜色不同切割开放入PictureBox中,然后改变PictureBox的形状保证和图一样,鼠标移进移出这个就随你了。这个方法有点像自定义窗体。
jackie615 2005-02-28
  • 打赏
  • 举报
回复
帮你up

16,549

社区成员

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

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