Hi @Mansour_Dalir ,
There is no such basic .NET class that can achieve.
You can refer to the following code, this method is straightforward but may be slow for large images or complex comparisons.
If you want more advanced and efficient image processing, you can find some third-party libraries.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mainImage As Bitmap = Bitmap.FromFile("path_to_main_image.jpg")
Dim template As Bitmap = Bitmap.FromFile("path_to_template_image.jpg")
Dim mainWidth As Integer = mainImage.Width
Dim mainHeight As Integer = mainImage.Height
Dim templateWidth As Integer = template.Width
Dim templateHeight As Integer = template.Height
' Loop through the main image to find the template
For x As Integer = 0 To mainWidth - templateWidth
For y As Integer = 0 To mainHeight - templateHeight
If CompareImages(mainImage, template, x, y) Then
' Match found
MessageBox.Show("Match found at X: " & x & ", Y: " & y)
' Draw a rectangle around the matching area
Dim g As Graphics = Graphics.FromImage(mainImage)
g.DrawRectangle(New Pen(Color.Red, 3), New Rectangle(x, y, templateWidth, templateHeight))
PictureBox1.Image = mainImage
g.Dispose()
Return
End If
Next
Next
MessageBox.Show("No match found")
End Sub
Private Function CompareImages(mainImage As Bitmap, template As Bitmap, startX As Integer, startY As Integer) As Boolean
For x As Integer = 0 To template.Width - 1
For y As Integer = 0 To template.Height - 1
Dim mainPixel As Color = mainImage.GetPixel(startX + x, startY + y)
Dim templatePixel As Color = template.GetPixel(x, y)
' Check if pixels are similar (you can adjust the tolerance as needed)
If Not PixelsAreSimilar(mainPixel, templatePixel) Then
Return False
End If
Next
Next
Return True
End Function
Private Function PixelsAreSimilar(pixel1 As Color, pixel2 As Color, Optional tolerance As Integer = 50) As Boolean
' Compare the RGB values of the pixels within a tolerance
Return Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) <= tolerance AndAlso
Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) <= tolerance AndAlso
Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) <= tolerance
End Function
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.