6 Replies - 2535 Views - Last Post: 23 June 2009 - 08:33 PM Rate Topic: -----

#1 sonia.sardana   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 132
  • Joined: 01-June 08

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

Posted 08 June 2009 - 12:19 PM

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

Is This A Good Question/Topic? 0
  • +

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

#2 kasbaba   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 134
  • Joined: 03-November 08

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

Posted 08 June 2009 - 12:53 PM

Hi,

try this : Convert C# to VB.NET

hope this helps..

____________________
Was this helpful. Click on THIS POST WAS HELPFUL
Was This Post Helpful? 0
  • +
  • -

#3 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

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

Posted 08 June 2009 - 01:28 PM

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 :) ).
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.
Was This Post Helpful? 0
  • +
  • -

#4 Ändrew   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 312
  • Joined: 21-April 08

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

Posted 09 June 2009 - 05:02 AM

I agree aliens only like finding firm brains like ours.
Was This Post Helpful? 0
  • +
  • -

#5 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

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

Posted 09 June 2009 - 07:59 AM

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 ";" .
(byte[])converter.ConvertTo(bmp1, imgBytes2.GetType());


Heres my corrections.

	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 

Was This Post Helpful? 0
  • +
  • -

#6 sonia.sardana   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 132
  • Joined: 01-June 08

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

Posted 09 June 2009 - 12:57 PM

Thx very much...Especially to T3hC13h....

Another method
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: 09 June 2009 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 woodjom   User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 566
  • Joined: 08-May 08

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

Posted 23 June 2009 - 08:33 PM

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 June 2009 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1