Results 1 to 3 of 3

Thread: Convert VB to VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    1

    Convert VB to VBA

    Hi I need convert from VB to VBA this Code :
    Code:
    Private Sub btnCheck_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnGo.Click
        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
    
        ' Get the threshold.
        Dim threshold As Integer = _
            Integer.Parse(txtThreshold.Text)
    
        ' Load the images.
        Dim bmp1 As Bitmap = Image.FromFile(txtFile1.Text)
        Dim bmp2 As Bitmap = Image.FromFile(txtFile2.Text)
    
        ' Make a difference image.
        Dim wid As Integer = Math.Min(bmp1.Width, bmp2.Width)
        Dim hgt As Integer = Math.Min(bmp1.Height, bmp2.Height)
        Dim bmp3 As New Bitmap(wid, hgt)
    
        ' Create the difference image.
        Dim are_identical As Boolean = True
        Dim r1, g1, b1, r2, g2, b2, r3, g3, b3 As Integer
        Dim color1, color2 As Color
        Dim eq_color As Color = Color.White
        Dim ne_color As Color = Color.Red
        Dim dr, dg, db, diff As Integer
        For x As Integer = 0 To wid - 1
            For y As Integer = 0 To hgt - 1
                color1 = bmp1.GetPixel(x, y)
                color2 = bmp2.GetPixel(x, y)
                dr = CInt(color1.R) - color2.R
                dg = CInt(color1.G) - color2.G
                db = CInt(color1.B) - color2.B
                diff = dr * dr + dg * dg + db * db
                If diff <= threshold Then
                    bmp3.SetPixel(x, y, eq_color)
                Else
                    bmp3.SetPixel(x, y, ne_color)
                    are_identical = False
                End If
            Next y
        Next x
    
        ' Display the result.
        picResult.Image = bmp3
    
        Me.Cursor = Cursors.Default
        If (bmp1.Width <> bmp2.Width) OrElse (bmp1.Height <> _
            bmp2.Height) Then are_identical = False
        If are_identical Then
            MessageBox.Show("The images are identical")
        Else
            MessageBox.Show("The images are different")
        End If
    
        bmp1.Dispose()
        bmp2.Dispose()
    End Sub
    Last edited by Shaggy Hiker; Jan 14th, 2020 at 04:17 PM. Reason: Added CODE tags.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,514

    Re: Convert VB to VBA

    You shouldn't put your code inside the quote tabs, it loses all the formatting. Use the VB or # , it will make it much easier for us to read.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,126

    Re: Convert VB to VBA

    I swapped the QUOTE tags for CODE tags (the # button) to clean up the display.

    There's no magic means to convert .NET to VBA that I'm aware of, nor the other way around, either. However, the two languages are pretty similar, so a good place to start might be to just copy and paste the code then fix the differences.

    You can get rid of the DoEvents call. That doesn't do anything at all in the method shown, as there simply can't be any pending messages to process at that time.

    You can change Integer.Parse to CInt(), which I think is what VBA would be using. Neither one is great, but they are bad in the same way, so if Integer.Parse was working, then CInt will work as well.

    Integers have a different size in VBA, but it looks like that shouldn't matter much for you. Diff might need to be Long. The other items are really just bytes, so they'll fit just fine into Integers no matter what size the integer is.

    You'll likely have some kind of issue with loading the bitmaps from the files, as that is likely different in VBA. I don't know how VBA does it, though I'm sure there is a way. You might also have issues with GetPixel and SetPixel, since they may be different in VBA. Still, you can try the copy and paste and just deal with the errors as they arise.

    I believe GetPixel and SetPixel may be terribly slow for this, too. That's a different issue, though, especially if those two methods don't exist in VBA. If they DO exist in VBA, you might want a faster image processing mechanism. Not sure what would be available in VBA.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width