VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,336 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,655 people online right now. Registration is fast and FREE... Join Now!




Help needed in Converting C# Code to VB.Net Code

 

Help needed in Converting C# Code to VB.Net Code

sonia.sardana

8 Jun, 2009 - 11:19 AM
Post #1

D.I.C Head
**

Joined: 1 Jun, 2008
Posts: 132



Thanked: 5 times
My Contributions
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







User is offlineProfile CardPM
+Quote Post


kasbaba

RE: Help Needed In Converting C# Code To VB.Net Code

8 Jun, 2009 - 11:53 AM
Post #2

D.I.C Head
**

Joined: 3 Nov, 2008
Posts: 90



Thanked: 14 times
My Contributions
Hi,

try this : Convert C# to VB.NET

hope this helps..

____________________
Was this helpful. Click on THIS POST WAS HELPFUL
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Help Needed In Converting C# Code To VB.Net Code

8 Jun, 2009 - 12:28 PM
Post #3

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
My first line of suggestion is to have a rather comfortable knowledge of VB and C#. Variable declarations differ in both and understanding how one declares a object, method, class, namespace will help you understand how the translation between the 2 will happen.

Key Point:
There are aspects of C# that VB just cannot do under any amount of developing (until .Net 4 that is smile.gif ).
for example: C# is by extension C++ with CLR capabilities inheretted, therefore there is an explicit difference between variable 'c' and 'C'. but in VB, variable 'c' and 'C' are one in the same.

If you are not confortable with VB, then i would not suggest trying to learn C# until you are confortable with being confused with VB. Once you hit the point of being able the confuse yourself utterly with VB, then at that point add C# to the confusion to make your brain HULU mush for the aliens.
User is offlineProfile CardPM
+Quote Post

Ändrew

RE: Help Needed In Converting C# Code To VB.Net Code

9 Jun, 2009 - 04:02 AM
Post #4

D.I.C Head
Group Icon

Joined: 21 Apr, 2008
Posts: 232



Thanked: 10 times
Dream Kudos: 75
My Contributions
I agree aliens only like finding firm brains like ours.
User is offlineProfile CardPM
+Quote Post

T3hC13h

RE: Help Needed In Converting C# Code To VB.Net Code

9 Jun, 2009 - 06:59 AM
Post #5

D.I.C Head
**

Joined: 5 Feb, 2008
Posts: 153



Thanked: 25 times
My Contributions
Your trying to use a C# style array declaration, see below for the correct way to declare an array in VB.Net
Dim imgBytes1 As Byte() = New Byte(1)

The below line has two issues, first your trying to use a C# type converion and you ended the line with ";" .
CODE
(byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());


Heres my corrections.

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(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 And imgHash2.Length
            If ((imgHash1(i) <> imgHash2(i))) Then
                doImagesMatch = False
            Else
                doImagesMatch = True
            End If
        Next
    End Function

User is offlineProfile CardPM
+Quote Post

sonia.sardana

RE: Help Needed In Converting C# Code To VB.Net Code

9 Jun, 2009 - 11:57 AM
Post #6

D.I.C Head
**

Joined: 1 Jun, 2008
Posts: 132



Thanked: 5 times
My Contributions
Thx very much...Especially to T3hC13h....

Another 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.....

This post has been edited by sonia.sardana: 9 Jun, 2009 - 11:58 AM
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Help Needed In Converting C# Code To VB.Net Code

23 Jun, 2009 - 07:33 PM
Post #7

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 365



Thanked: 15 times
My Contributions
If i remember right as i browsed the image class...there should be a contains function but it operates at the byte level. It is not a 100% guarantee. In fact of all the image comparison codes i have seen, there is not one single image comparison method that produces anything higher than a 80% probability. Most give you a success of true if around 60% of the 2 images match. And most take a colord image and put it to black and white and then compare, this way it can compare solid white and solid black areas for the probability.

If you want it to compare 100% you will have to iterate the number of image bytes in the image being used to compare and then start at position 0 of the source image and compare it byte to byte until you finish. Good luck on this one as you will be crunching bytes for forever pending on the size and bit spectrum the image can support.

Also, you return false upon the first unsuccessful match. if i was you i would create a variable that contains the number of matched bytes of the image and then divide it by the total length of the image and if its below 60% then False, else True.

This post has been edited by woodjom: 23 Jun, 2009 - 07:35 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:09PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month