讨论一个关于region的问题!
讨论一个关于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