0

Hi All,

Can anyone offer me some advise? I currently have a listbox i am using, in the listbox there is a list of images from any website. they are grabbed from the website via this method;;;

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
    For Each CurElement As HtmlElement In PageElements
        imagestxt.Items.Add(imagestxt.Text & CurElement.GetAttribute("src") & Environment.NewLine)
    Next
    Timer1.Enabled = True
End Sub

I then use the picture control method to get the image and display it.

pic1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(imagestxtimagestxt.SelectedItem.ToString))).SelectedItem.ToString)))

This method pulls the images and title from the HTML.

Private Function StrHTML12() As Boolean
    Dim htmlDocument As HtmlDocument = WebBrowser1.Document
    ListBox1.Items.Clear()
    For Each element As HtmlElement In htmlDocument.All
        ListBox1.Items.Add(element.TagName)
        If element.TagName.ToUpper = "IMG" Then
            imgtags.Items.Add(element.OuterHtml.ToString)
        End If
        If element.TagName.ToUpper = "TITLE" Then
            titletags.Items.Add(element.OuterHtml.ToString)
            Timer1.Enabled = False
        End If
    Next
End Function

This is a counting method to count how many empty alt="" or emply <img alt='' there are on the page.

  Dim strHTML As String
    strHTML = GetPageHTML(webaddresstxt.Text, 5)
    Dim counter, Count As Integer
    Count = 0
    counter = InStr(counter + 1, strHTML, "<img alt=''")
    counter = InStr(counter + 1, strHTML, "alt=''")
    counter = InStr(counter + 1, strHTML, "alt=""")
    If counter = 0 Then
        MsgBox(0)
        Exit Sub
    End If
    Do While counter > 0
        counter = InStr(counter + 1, strHTML, "<img alt=''")
    Loop
    MsgBox(Count)

Basicly what i am looking to do is;

1) Have a program that can check the image, look at the alt='' or <img alt='' if on the website the dev hasn't put anything in the alt tag i want the image to show in a picture box and i want the alt tag either next to it or underneith it or something. but i have no idea how.

2) counter = InStr(counter + 1, strHTML, "<img alt=''")
counter = InStr(counter + 1, strHTML, "alt=''")
counter = InStr(counter + 1, strHTML, "alt=""")

It seems really slow and messy. is there a better way of doing it?

counter = InStr(counter + 1, strHTML, "<img alt=''" OR "alt=''" OR "alt=""") - LOOKS MORE LIKE IT, however it dosn't work.

Can someone help me?

2
Contributors
7
Replies
35
Views
4 Years
Discussion Span
Last Post by tinstaafl
0

What could do is, in StrHTML12() check each element for an empty 'alt' attribute. Then process it from there.

BTW, StrHTML12() is declared as a function but it doesn't return anything.

0

Hi Tinstaafl,

Thanks for your reply. could you should be how i would do that in VB?

Thanks.

0

something like this:

Dim altvalue as String = element.GetAttribute("alt")
If altvalue = "" Or altvalue = "''" Then
    'From here you can increment your counter, check for 'IMG" tag and process the image, etc.
End If

Edited by tinstaafl

0

Thanks.

Is ther a way of only getting it to allow alt tags that are ""

I have tried this;

If element.TagName.ToUpper = "IMG" And element.GetAttribute("alt=""") Then
                    imgtags.Items.Add(element.OuterHtml.ToString)
                End If
0

The code I did will return every tag with an empty alt attribute. I would suggest using a string variable to store the value of the alt attribute. It makes checking it for "" much easier. I think i may have edited it a bit after you saw it.

Edited by tinstaafl

0

I would suggest using a string variable to store the value of the alt attribute

Sorry to sound so dumb but how do i do this?

I'm new on the programming block :(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.