Help converting Function to C#

Im a little lost to say the least.

Trying to compare 2 pictureboxes. Are they the same. Thanks

Public Shared Function ImagesAreTheSame(ByVal bmp1 _

As Bitmap, ByVal bmp2 As Bitmap) As Boolean

'If the images aren't the same size, quit

If bmp1.Size.Width <> bmp2.Size.Width OrElse _

bmp1.Size.Height <> bmp2.Size.Height Then

Return False

Else

'Convert each image to a byte array

'.NET.NET()

Dim ic As System.Drawing.ImageConverter = _

New System.Drawing.ImageConverter

Dim btImage1() As Byte = New Byte(1) {}

btImage1 = CType(ic.ConvertTo(bmp1, _

btImage1.GetType()), Byte())

Dim btImage2() As Byte = New Byte(1) {}

btImage2 = CType(ic.ConvertTo(bmp2, _

btImage2.GetType()), Byte())

'The byte arrays may be different lengths if they

'contain different metadata, e.g. EXIF, TIFF tags

If btImage1.Length <> btImage2.Length _

Then Return False

Dim i As Integer

For i = 0 To btImage1.Length - 1

If btImage1(i) <> btImage2(i) Then Return False

Next

Return True

End If

End Function




Answer this question

Help converting Function to C#

  • greekTowner

    Is this your whole program   Did you define a Main, and a class to hold this function

  • iestudi

    Probably so.  It's typically much easier to use a book/online examples and start from scratch rather than convert a VB.NET program.  Converting a program is typically more difficult than writing something basic from scratch.

  • XMLHacker

    If you send the errors you're receiving and some other code, I can certainly help out ...

    Josh

  • MSA37646

    Im very lost....this wont compile...

    public Boolean ImagesAreTheSame(Bitmap bmp1, Bitmap bmp2)

    {

    //If the images aren't the same size, quite

    if ((bmp1.Size.Width != bmp2.Size.Width) || (bmp1.Size.Height != bmp2.Size.Height))

    {

    return false;

    }

    else

    {

    //Convert each image to a byte array

    //.NET.NET()

    System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();

    Byte btImage1 = new Byte(1);

    btImage1 = CType(ic.ConvertTo(bmp1, btImage1.GetType()), Byte());

    Byte btImage2 = new Byte(1);

    btImage2 = CType(ic.ConvertTo(bmp2,btImage2.GetType()), Byte());

    if (btImage1.Length != btImage2.Length)

    return False;

    int i;

    for (i = 0; i < btImage1.Length - 1; i++)

    if (btImage1(i)!= btImage2(i))

    return False;

    }



  • nsimeonov



    public Boolean ImagesAreTheSame(Bitmap bmp1, Bitmap bmp2)
    {
       //If the images aren't the same size, quite
       if ((bmp1.Size.Width <> bmp2.Size.Width) || (bmp1.Size.Height <> bmp2.Size.Height))
       {
          return false;
       }
       else
       {
          //Convert each image to a byte array
          //.NET.NET()
          System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();
     .
     .
     .
    etc.

     


    Follow basic method above for converting rest of your code.

    Hope this helps,
    Josh



  • Or Mintz

    Think this works....just converting the calls to c#

    private bool CompareBitmaps(Bitmap bmp1, Bitmap bmp2)

    {

    if ((bmp1.Size.Width != bmp2.Size.Width) || (bmp1.Size.Height != bmp2.Size.Height))

    return false;

    else

    {

    System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();

    byte[] btImage1 = new byte[2];

    btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());

    byte[] btImage2 = new byte[2];

    btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());

    if (btImage1.Length != btImage2.Length)

    return false;

    for (int i = 0; i < btImage1.Length; i++)

    if (btImage1Idea != btImage2Idea)

    return false;

    return true;

    }

    }



  • Pardasani

    What errors do you receive ... also, why are you trying to convert the code to C# if you already know VB.NET

    Thanks,
    Josh Lindenmuth

  • Mark Redman

    trying to learn c#....as far as errors....just about every line.

    no constructors, type does'nt exit, byte is a type but used like a variable....on and on.

    I figured this was a good example of a "hairy" example to get into....maybe a little much...

  • elfranko

    Yes...its part of a class

  • Elisabeth Boonin

    private bool CompareBitmaps(Bitmap bmp1, Bitmap bmp2)
    {
     if (bmp1.Size.Width != bmp2.Size.Width || bmp1.Size.Height != bmp2.Size.Height) {
       return false;
     } else {
       System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();
       byte[][0] btImage1 = new byte[1];
       btImage1 = ((byte)(ic.ConvertTo(bmp1, btImage1.GetType())));
       byte[][0] btImage2 = new byte[1];
       btImage2 = ((byte)(ic.ConvertTo(bmp2, btImage2.GetType())));
       if (btImage1.Length != btImage2.Length) {
         return false;
       }
       int i;
       for (int i = 0; i <= btImage1.Length - 1; i++) {
         if (btImage1(i) != btImage2(i)) {
           return false;
         }
       }
       return true;
     }
    }


  • Help converting Function to C#