search one image in windowsform use vb.net

Category: visual studio vb

Question

insiderTool on Tue, 13 Dec 2016 13:21:53


How to  open one image to picturebox 1 and find it in sql sever( compare 2 image) and show image same to picturebox2.

Replies

tommytwotrain on Tue, 13 Dec 2016 14:25:26


I don't know about what you have with the sql but here is one way to compare two images.

Option Strict On Imports System.Drawing.Imaging Public Class Form7 Private sw As New Stopwatch Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load PictureBox1.BackgroundImage = Image.FromFile("c:\bitmaps\rusty.jpg") PictureBox1.BackgroundImageLayout = ImageLayout.Zoom

'this image matches PictureBox2.BackgroundImage = Image.FromFile("c:\bitmaps\rusty.jpg")

'this image does not match 'PictureBox2.BackgroundImage = Image.FromFile("c:\bitmaps\rusty1.bmp")

PictureBox2.BackgroundImageLayout = ImageLayout.Zoom Label1.AutoSize = False Label1.Text = "" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim SameImage As Boolean = True Dim x, y As Integer sw.Reset() sw.Start() Using bmp1 As Bitmap = CType(PictureBox1.BackgroundImage, Bitmap), bmp2 As Bitmap = CType(PictureBox2.BackgroundImage, Bitmap) 'see if they are the same size If bmp2.Width = bmp1.Width AndAlso bmp2.Height = bmp1.Height Then

'see if the colors match - adjust step for accuracy Dim thestep As Integer = CInt(bmp1.Width / 10) For x = 0 To bmp1.Width Step thestep For y = 0 To bmp1.Height Step thestep If bmp1.GetPixel(x, y) <> bmp2.GetPixel(x, y) Then SameImage = False Exit For End If Next Next Else SameImage = False End If End Using sw.Stop() If SameImage Then Label1.Text = "Images Match: x = " & x.ToString & " y = " & y.ToString & vbLf & "Time: " & sw.ElapsedTicks.ToString Else Label1.Text = "Images Do Not Match: x = " & x.ToString & " y = " & y.ToString & vbLf & "Time: " & sw.ElapsedTicks.ToString End If Label1.Refresh() End Sub End Class



Kareninstructor on Tue, 13 Dec 2016 17:42:36


Since a compare has been mentioned, here is how you might look at retrieval of an image.  Write your SQL SELECT, in this case we want the primary key and image

SELECT ImageID, ImageData FROM ImageData

Use SqlClient, connection and command to load the data into a DataTable. Now iterate each DataRow in the DataTable. Let's say the DataTable name is dtResults, we would use something like this to get at the image

For x As Integer = 0 To dtResults.Rows.Count - 1
    Using theStream As New MemoryStream(CType(dtResults.Rows(x)("ImageData"), Byte()))
        Dim theImage = Image.FromStream(ms)
        ' do compare
    End Using
Next

Castorix31 on Tue, 13 Dec 2016 18:18:06


BTW, to compare 2 images, you should use LockBits() instead of GetPixel()

(a lot faster if images are big)


Mr. Monkeyboy on Tue, 13 Dec 2016 18:26:22


How to  open one image to picturebox 1 and find it in sql sever( compare 2 image) and show image same to picturebox2.

Comparing 2 images needs to be explained.

Unless the 2 images are the exact same size and the exact same images then comparison requirements become substantially difficult.

For example if an image of a person is stored in a database and then you take a picture of that person with a camera which provides the image to a computer there isn't going to be a simple way to compare the stored image and the taken image to determine in some fashion if the two images contain the same person.

Also it is probably unlikely two different image formats (.Jpg and .Bmp for example) could be compared and an exact pixel by pixel match occur.

Even finger print image stored and then compared to another taken of same finger will not exactly match as the two prints will not be exact with regard to reference in either image and a comparison for those images would be difficult also.


La vida loca

tommytwotrain on Tue, 13 Dec 2016 18:47:08


BTW, to compare 2 images, you should use LockBits() instead of GetPixel()

(a lot faster if images are big)


LOL. Yes. However, you might be surprised. It Depends.

But I showed you mine now you show us yours. :)

Frank L. Smith on Tue, 13 Dec 2016 19:10:21


BTW, to compare 2 images, you should use LockBits() instead of GetPixel()

(a lot faster if images are big)


LOL. Yes. However, you might be surprised. It Depends.

But I showed you mine now you show us yours. :)

Tommy,

You know how I am with graphics, but this had me curious.

When you get time, have a look at this post:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/d80970b9-303a-4bf6-8b7f-7bdde93a2c00/how-to-make-comparison-between-pictures-in-vbnet?forum=vbgeneral

It's not short, but you'll definitely recognize many of the names in it. I like the way that John (Wein), suggested:

Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean

    Dim MS1 As New MemoryStream
    Dim MS2 As New MemoryStream
    I1.Save(MS1, ImageFormat.Bmp)
    I2.Save(MS2, ImageFormat.Bmp)

    For I As Integer = 0 To CInt(MS1.Length) - 1
      If MS1.ReadByte() <> MS2.ReadByte Then Return False
    Next

    Return True
End Function

I miss John, but - that aside - he always had a very unique way of looking at how to get things done in code. I think the above is no exception. :)

Your thoughts?

tommytwotrain on Tue, 13 Dec 2016 19:49:17


https://social.msdn.microsoft.com/Forums/vstudio/en-US/d80970b9-303a-4bf6-8b7f-7bdde93a2c00/how-to-make-comparison-between-pictures-in-vbnet?forum=vbgeneral

It's not short, but you'll definitely recognize many of the names in it. I like the way that John (Wein), suggested:

Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean

    Dim MS1 As New MemoryStream
    Dim MS2 As New MemoryStream
    I1.Save(MS1, ImageFormat.Bmp)
    I2.Save(MS2, ImageFormat.Bmp)

    For I As Integer = 0 To CInt(MS1.Length) - 1
      If MS1.ReadByte() <> MS2.ReadByte Then Return False
    Next

    Return True
End Function

I miss John, but - that aside - he always had a very unique way of looking at how to get things done in code. I think the above is no exception. :)

Your thoughts?


Frank,

Yes sure I miss JW too. Think of him every time I see a metafile.

I am not going to say much more as others know more about it. But, if I understand it, this routine of JW's saves the file to disc which I think one would avoid. I think this was the exact match version he showed in the thread. Not that I follow what they mean in the thread at this time. But like Monkey says it gets down to the exact conditions of your search requirements.

PS Oh wait its not to disc I guess. Well I guess this is fine and dandy. But depends.

Frank L. Smith on Tue, 13 Dec 2016 20:01:10


I see your edit but the sake of others:

...But, if I understand it, this routine of JW's saves the file to disc...

It's saving the image data (each) to the respective streams, so it's strictly memory; no IO involved.

*****

Following is my version of it as I kept seeing those streams left "wide open" ;-)

Option Strict On Option Explicit On Option Infer Off Imports System.IO.Path Public Class Form1 Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load Const baseDir As String = _ "K:\Personal Folders\Tim\Kauai 11-2006\106_1120" Dim imagePath1 As String = _ Combine(baseDir, "IMGP0964.JPG") Dim imagePath2 As String = _ Combine(baseDir, "IMGP0965.JPG") Dim test As Boolean = _ ImagesAreVisuallySimilar(Image.FromFile(imagePath1), _ Image.FromFile(imagePath2)) Stop End Sub ''' <summary> ''' A method which will return a boolean value indicating the ''' visual comparison of two images. ''' </summary> ''' <param name="image1">The first image to compare.</param> ''' <param name="image2">The second image to compare.</param> ''' <returns></returns> ''' <remarks>This code is a modified version of the code as ''' originally thought of and posted by John Wein in this thread: ''' https://social.msdn.microsoft.com/Forums/vstudio/en-US/d80970b9-303a-4bf6-8b7f-7bdde93a2c00/how-to-make-comparison-between-pictures-in-vbnet?forum=vbgeneral</remarks> Private Function _ ImagesAreVisuallySimilar(ByVal image1 As Image, _ ByVal image2 As Image) As Boolean Dim retVal As Boolean = False Try If image1 Is Nothing Then Throw New ArgumentNullException("Image 1", "Cannot be null." & vbCrLf) ElseIf image2 Is Nothing Then Throw New ArgumentNullException("Image 2", "Cannot be null." & vbCrLf) Else If image1.Width = image2.Width AndAlso image1.Height = image2.Height Then Using ms1 As New IO.MemoryStream image1.Save(ms1, Imaging.ImageFormat.Bmp) Using ms2 As New IO.MemoryStream image2.Save(ms2, Imaging.ImageFormat.Bmp) For i As Integer = 0 To CInt(ms1.Length) - 1 If ms1.ReadByte <> ms2.ReadByte Then Exit Function End If Next retVal = True End Using End Using End If End If Catch ex As Exception MessageBox.Show(String.Format("An error occurred:{0}{0}{1}", vbCrLf, ex.Message), _ "Exception Thrown", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try Return retVal End Function End Class


If you'd care to, try it yourself and for the first test, give it the same image both times. It should come back as true (it does here).

I then tried it on two different images:

It returned false.

Try it out if you'd care to. :)