| | |
Help needed in Converting C# Code to VB.Net Code
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 324
Reputation:
Solved Threads: 7
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
Mine VB.net Converted Coding-
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
C# EXAMPLE
VB.NET Syntax (Toggle Plain Text)
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-
VB.NET Syntax (Toggle Plain Text)
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
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)
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
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
![]() |
Similar Threads
- How to draw a line in a VB.NET form? (VB.NET)
- vb.net code for searching (VB.NET)
- Moving VB.NET Code to ASP.NET Code? (ASP.NET)
- convert vb6 code to vb.net (VB.NET)
- How to convert VB6 code to .net code (VB.NET)
- How to change php code html code and code like this ? (Existing Scripts)
- Classic to .net migration question (ASP.NET)
- VB.NET Code (ASP.NET)
- Which of the .NET Framework Languages Is Best? (ASP.NET)
- suitability of .net for tcp/ip server (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: WebBrowser
- Next Thread: Syntax error in SQL update string
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic beginner browser button buttons center check code component connectionstring crystalreport cuesent data database databasesearch datagrid datagridview date datetimepicker design designer dissertation dissertations dissertationthesis dissertationtopic dropdownlist excel fade file-dialog filter folder forms ftp generatetags hardcopy html image images input insert intel monitor net networking open output panel passingparameters peertopeervideostreaming picturebox picturebox1 port printing problem problemwithinstallation project reports" searchvb.net select settings shutdown sqlserver survey tcp temperature text textbox timespan toolbox transparency trim updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





