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

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

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

 
0
  #1
Jun 8th, 2009
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
  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-
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 27
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

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

 
0
  #2
Jun 8th, 2009
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:

  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
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

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

 
0
  #3
Jun 9th, 2009
The link that you gave is not converting the code 100% Correctly...But sir ur code is working 100 % Correctly....Thx very Much.....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

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

 
0
  #4
Jun 9th, 2009
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....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC