Resolved Use image outline as region for hittest

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I have a function that gets the outline of a picture with a tranparent background and returns it as a region.

My problem is that the region returned is not in the same place as the original image.

If I check the region by filling it with color the region appears in the upper left corner.

How can I fix this?

Here are the relevant parts of the code I use:

PrivateSub FrmBackgroundImage()


Dim FrmImageBitmap AsNewBitmap(Me.Width, Me.Height)
Dim imagefile AsGraphics = graphics.FromImage(FrmImageBitmap)


'add image
imagefile.DrawImage(testImage, 50, 103)


' Use as formbackgroundImage
Me.BackgroundImage = FrmImageBitmap


EndSub


PrivateFunction GetRegion(ByVal _img AsBitmap, ByVal color__1 AsColor) AsRegion


Dim _matchColor AsColor = Color.FromArgb(color__1.A)
rgn = NewRegion()
rgn.MakeEmpty()
Dim rc AsNewRectangle(0, 0, 0, 0)
Dim inimage AsBoolean = False


For y AsInteger = 0 To _img.Height - 1


For x AsInteger = 0 To _img.Width - 1


IfNot inimage Then


If _img.GetPixel(x, y) <> _matchColor Then


inimage = True
rc.X = x
rc.Y = y
rc.Height = 1


EndIf


Else


If _img.GetPixel(x, y) = _matchColor Then
inimage = False
rc.Width = x - rc.X


rgn.Union(rc)


EndIf


EndIf


Next


If inimage Then


inimage = False
rc.Width = _img.Width - rc.X
rgn.Union(rc)


EndIf


Next


Return rgn


EndFunction






PrivateSub hittestStartlane()


Dim PN AsPointF = NewPointF(ulcorner.X + 24, ulcorner.Y – 8) 'is point of moving object


GetRegion(topleftimage, Color_1)


I****N = rgn.IsVisible(PN)


If I****N = True Then


Beep()


'the folowing lines are only used to check if region(rgn) exists and where it is.


Dim e AsGraphics = Me.CreateGraphics


Dim pen As System.Drawing.SolidBrush = NewSolidBrush(Color.DarkGreen)


e.FillRegion(pen, rgn)



EndIf

EndSub


Thanks in advance,


Groover
 
Last edited:
Hi all,

I found the solution myself.

I use

GetRegion(topleftimage, Color_1)
rgn.Translate(x,y)
x and y are the coordinates of the topleft corner of the image.

to move the region to where the image is.

Groover
 
Back
Top