Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-08-2009, 4:20 PM
VB.NET Forum Enthusiast
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jan 2009
Age: 24
Posts: 34
Reputation: 15
sonia.sardana is on a distinguished programming path ahead
Default Help needed in Converting C# Code to VB.Net Code

Hey frnds, I m comparing two images in VB.Net..I get the code in C# & dat code is working 100 % Correctly..Now want to convert dat code to vb.net..& i need help in just converting two lines-

C# EXAMPLE
Code:
  private void Form1_Load(object sender, EventArgs e)
        {
            
              Bitmap img1= new Bitmap ("D:\\Documents and Settings\\Sonia\\Desktop\\sonia1.bmp");
              Bitmap img2 = new Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia2.bmp");
              bool a;
           a= doImagesMatch(img1,img2 );
        }

     
            public bool doImagesMatch( Bitmap bmp1,  Bitmap bmp2)
            {
            try
                {
               
                 //each image to a byte array
                 ImageConverter converter = new ImageConverter();
                 //create 2 byte arrays, one for each image
                 byte[] imgBytes1 = new byte[1];
                 byte[] imgBytes2 = new byte[1];
        
              //convert images to byte array
              imgBytes1 = (byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());
              imgBytes2 = (byte[])converter.ConvertTo(bmp2, imgBytes1.GetType());
                
            //now compute a hash for each image from the byte arrays
            SHA256Managed sha = new SHA256Managed();
            byte[] imgHash1 = sha.ComputeHash(imgBytes1);
            byte[] imgHash2 = sha.ComputeHash(imgBytes2);
                

            //now let's compare the hashes

            for (int i = 0; i < imgHash1.Length && i < imgHash2.Length; i++)
             {
                if (!(imgHash1[i] == imgHash2[i]))
                return false;

        }
    }

            catch (Exception ex)
               {
              MessageBox.Show(ex.Message);
             return false;
               }
                    return true;
                } 
    }
Mine VB.net Converted Coding-
Code:
 Public Function doImagesMatch(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
        Dim converter As ImageConverter = New ImageConverter()
        Dim i As Integer
        Dim imgBytes1 As Byte() = New Byte(1)
        Dim imgBytes2 As Byte() = New Byte(1)
    imgBytes1 = (byte())converter.ConvertTo(bmp1, imgBytes2.GetType());

        imgBytes2 = (byte())converter.ConvertTo(bmp2, imgBytes1.GetType());
        Dim sha As SHA256Managed = New SHA256Managed()

        Dim imgHash1 As Byte() = sha.ComputeHash(imgBytes1)

        Dim imgHash2 As Byte() = sha.ComputeHash(imgBytes2)
        For i = 0 To imgHash1.Length And imgHash2.Length
            If ((imgHash1(i) <> imgHash2(i))) Then
                doImagesMatch = False
            Else
                doImagesMatch = True
            End If
        Next

    End Function
ERRORS IN MINE CODE-
Dim imgBytes1 As Byte() = New Byte(1) - Type Byte has no constructors
imgBytes1 = (byte())converter.ConvertTo(bmp1, imgBytes2.GetType()) -- 'Byte' is a type and cannot be used as an expression, '.' expected
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-08-2009, 4:50 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 816
Reputation: 459
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

Code:
Dim imgBytes1() As Byte = New Byte(1) {}
imgBytes1 = CType(converter.ConvertTo(bmp1, imgBytes2.GetType()), Byte())
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-09-2009, 3:51 AM
Robert_Zenz's Avatar
VB.NET Forum Idol
.NET Framework: .NET 2.0
 
Join Date: Jun 2008
Location: Vienna, Austria
Age: 22
Posts: 503
Reputation: 289
Robert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NET
Default

Code:
        Dim imgBytes1() As Byte = {1}
        Dim imgBytes2() As Byte = {1}
__________________
Don't give TypeCasting Errors a chance, turn ON Option Strict!
Greatest Obfuscator ever: EazFuscator (Freeware)
Greatest Reflection Tool ever: .NET Reflector (Freeware) with Add-Ins
Greatest Introspection Tool ever: Gendarme (GPL)
Greatest MySQL FrontEnd ever: MySQL-Front (Shareware), HeidiSQL (GPL)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-09-2009, 5:33 AM
grmbl's Avatar
VB.NET Forum Enthusiast
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Jun 2009
Location: Belgium
Age: 27
Posts: 32
Reputation: 11
grmbl is on a distinguished programming path ahead
Default

You can always convert C# to VB.net using the following link:

ConvertCSharp2VB
__________________
"The world is coming to an end... SAVE YOUR BUFFERS !"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-09-2009, 5:02 PM
VB.NET Forum Enthusiast
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jan 2009
Age: 24
Posts: 34
Reputation: 15
sonia.sardana is on a distinguished programming path ahead
Default

Thx very much but ...Conversion process didnt work 100 % Corectly..

Tested & complete Code-
FIRST METHOD
Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim img1 As Bitmap = Image.FromFile("D:\Documents and Settings\Sonia\Desktop\sonia1.bmp")
        Dim img2 As Bitmap = Image.FromFile("D:\Documents and Settings\Sonia\Desktop\sonia2.bmp")
        Dim a As Boolean
        a = doImagesMatch(img1, img2)
    End Sub

    Public Function doImagesMatch(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
        Dim converter As ImageConverter = New ImageConverter()
        Dim i As Integer
        Dim imgBytes1(0) As Byte
        Dim imgBytes2(0) As Byte
        imgBytes1 = CType(converter.ConvertTo(bmp1, imgBytes1.GetType()), Byte())

        imgBytes2 = CType(converter.ConvertTo(bmp2, imgBytes1.GetType()), Byte())
        Dim sha As SHA256Managed = New SHA256Managed()

        Dim imgHash1 As Byte() = sha.ComputeHash(imgBytes1)

        Dim imgHash2 As Byte() = sha.ComputeHash(imgBytes2)
        For i = 0 To imgHash1.Length - 1 And imgHash2.Length - 1
            If ((imgHash1(i) <> imgHash2(i))) Then
                doImagesMatch = False
            Else
                doImagesMatch = True
            End If
        Next
    End Function
SECOND METHOD
Code:
Dim img1 As Bitmap = New Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia1.bmp")
Dim img2 As Bitmap = New Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia2.bmp")
Dim a As Boolean
a= doImagesMatch(img1,img2)
End Sub

Public Function doImagesMatch(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
Try

'each image to a byte array

Dim converter As ImageConverter = New ImageConverter()
'create 2 byte arrays, one for each image
Dim imgBytes1() As Byte = New Byte(1) {}
Dim imgBytes2() As Byte = New Byte(1) {}

'convert images to byte array
imgBytes1 = CType(converter.ConvertTo(bmp1, imgBytes2.GetType()), Byte())
imgBytes2 = CType(converter.ConvertTo(bmp2, imgBytes1.GetType()), Byte())

'now compute a hash for each image from the byte arrays

Dim sha As SHA256Managed = New SHA256Managed()
Dim imgHash1() As Byte = sha.ComputeHash(imgBytes1)
Dim imgHash2() As Byte = sha.ComputeHash(imgBytes2)

'now let's compare the hashes

Dim i As Integer
For i = 0 To imgHash1.Length And i < imgHash2.Length- 1 Step i + 1
If Not (imgHash1(i) = imgHash2(i)) Then
Return False
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
Return True
End Function
Hey i want to ask that frnds Suppose if i have mine full image..& suppose i edit it with paint & cut it half..& then compare full image & half image...then function returns fals....I want to just ask dat can we do i Vb.net..to compare two images one full & one half...& still we get true.....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 12:58 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.