943,563 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 935
  • VB.NET RSS
Jun 8th, 2009
0

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

Expand Post »
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
VB.NET Syntax (Toggle Plain Text)
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.  
  4. Bitmap img1= new Bitmap ("D:\\Documents and Settings\\Sonia\\Desktop\\sonia1.bmp");
  5. Bitmap img2 = new Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia2.bmp");
  6. bool a;
  7. a= doImagesMatch(img1,img2 );
  8. }
  9.  
  10.  
  11. public bool doImagesMatch( Bitmap bmp1, Bitmap bmp2)
  12. {
  13. try
  14. {
  15.  
  16. //each image to a byte array
  17. ImageConverter converter = new ImageConverter();
  18. //create 2 byte arrays, one for each image
  19. byte[] imgBytes1 = new byte[1];
  20. byte[] imgBytes2 = new byte[1];
  21.  
  22. //convert images to byte array
  23. imgBytes1 = (byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());
  24. imgBytes2 = (byte[])converter.ConvertTo(bmp2, imgBytes1.GetType());
  25.  
  26. //now compute a hash for each image from the byte arrays
  27. SHA256Managed sha = new SHA256Managed();
  28. byte[] imgHash1 = sha.ComputeHash(imgBytes1);
  29. byte[] imgHash2 = sha.ComputeHash(imgBytes2);
  30.  
  31.  
  32. //now let's compare the hashes
  33.  
  34. for (int i = 0; i < imgHash1.Length && i < imgHash2.Length; i++)
  35. {
  36. if (!(imgHash1[i] == imgHash2[i]))
  37. return false;
  38.  
  39. }
  40. }
  41.  
  42. catch (Exception ex)
  43. {
  44. MessageBox.Show(ex.Message);
  45. return false;
  46. }
  47. return true;
  48. }
  49. }

Mine VB.net Converted Coding-
VB.NET Syntax (Toggle Plain Text)
  1. Public Function doImagesMatch(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
  2. Dim converter As ImageConverter = New ImageConverter()
  3. Dim i As Integer
  4. Dim imgBytes1 As Byte() = New Byte(1)
  5. Dim imgBytes2 As Byte() = New Byte(1)
  6. imgBytes1 = (byte())converter.ConvertTo(bmp1, imgBytes2.GetType());
  7.  
  8. imgBytes2 = (byte())converter.ConvertTo(bmp2, imgBytes1.GetType());
  9. Dim sha As SHA256Managed = New SHA256Managed()
  10.  
  11. Dim imgHash1 As Byte() = sha.ComputeHash(imgBytes1)
  12.  
  13. Dim imgHash2 As Byte() = sha.ComputeHash(imgBytes2)
  14. For i = 0 To imgHash1.Length And imgHash2.Length
  15. If ((imgHash1(i) <> imgHash2(i))) Then
  16. doImagesMatch = False
  17. Else
  18. doImagesMatch = True
  19. End If
  20. Next
  21.  
  22. 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
Similar Threads
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Jun 8th, 2009
0

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

I used this converter: http://www.dotnetspider.com/convert/Csharp-To-Vb.aspx. It seems to compile the app then use the MSIL to convert it to VB, which is the best way of translation. If this doesn't work, I can manually translate it, so let me know. This is the result:

vbnet Syntax (Toggle Plain Text)
  1. Dim img1 As Bitmap = New Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia1.bmp")
  2. Dim img2 As Bitmap = New Bitmap("D:\\Documents and Settings\\Sonia\\Desktop\\sonia2.bmp")
  3. Dim a As Boolean
  4. a= doImagesMatch(img1,img2)
  5. End Sub
  6.  
  7.  
  8. Public Function doImagesMatch(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As Boolean
  9. Try
  10.  
  11. 'each image to a byte array
  12. Dim converter As ImageConverter = New ImageConverter()
  13. 'create 2 byte arrays, one for each image
  14. Dim imgBytes1() As Byte = New Byte(1) {}
  15. Dim imgBytes2() As Byte = New Byte(1) {}
  16.  
  17. 'convert images to byte array
  18. imgBytes1 = CType(converter.ConvertTo(bmp1, imgBytes2.GetType()), Byte())
  19. imgBytes2 = CType(converter.ConvertTo(bmp2, imgBytes1.GetType()), Byte())
  20.  
  21. 'now compute a hash for each image from the byte arrays
  22. Dim sha As SHA256Managed = New SHA256Managed()
  23. Dim imgHash1() As Byte = sha.ComputeHash(imgBytes1)
  24. Dim imgHash2() As Byte = sha.ComputeHash(imgBytes2)
  25.  
  26.  
  27. 'now let's compare the hashes
  28.  
  29. Dim i As Integer
  30. For i = 0 To imgHash1.Length And i < imgHash2.Length- 1 Step i + 1
  31. If Not (imgHash1(i) = imgHash2(i)) Then
  32. Return False
  33. End If
  34.  
  35. Next
  36. Catch ex As Exception
  37. MessageBox.Show(ex.Message)
  38. Return False
  39. End Try
  40. Return True
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jun 9th, 2009
0

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

The link that you gave is not converting the code 100% Correctly...But sir ur code is working 100 % Correctly....Thx very Much.....
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Jun 9th, 2009
0

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

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....
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: WebBrowser
Next Thread in VB.NET Forum Timeline: Syntax error in SQL update string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC