CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jun 2006
    Posts
    148

    Web Browser and Image Elements

    Hi all,

    I am trying to carry out the images from a offline webpage loaded in the webbrowser control.

    Following is the code.

    Code:
    WebBrowser wb = new WebBrowser();
    wb.Navigate("Path of the offline web page [ImageTest.htm]");
    HtmlElementCollection hec = wb.Document.Images;
    foreach (HtmlElement he in hec)
    {
          string xstr = he.GetAttribute("src");
          
          // Processing related to image
          ...............
          
          ...............
    }
    My problem is that the statement
    Code:
    HtmlElementCollection hec = wb.Document.Images
    returns zero images or no ElementCollection in the HtmlElementCollection.

    so i am not getting any image, though my offline document is having images.

    Now interesting thing is that when i simplify the webpage by editing it, i can get the image in the collection.

    Both the Normal and modified page are in the attachment.
    ImageTest.htm is the original page.
    ImageTest1.htm is the modified page.

    Thanx for any kind of help.
    Regards,
    Shail2k4
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    You're going about it the wrong way

    You need to use the HtmlElementCollection object with the GetElementsByTagName method, as in :

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                HtmlElementCollection imgs = this.webBrowser1.Document.GetElementsByTagName("img");
    
                for (int i = 0; i < imgs.Count; i++)
                {
                    wc.DownloadFile(imgs[i].GetAttribute("src"), "c:\\images" + i.ToString() + ".jpg");
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.webBrowser1.Navigate("C:\\imagetest.htm");
            }
    This downloads all the pictures to a folder on your C drive.

  3. #3
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Thanx for the prompt reply.

    I tried with your code too, but resulted into the same problem.

    Code:
    for (int i = 0; i < imgs.Count; i++)
    imgs.Count returns 0.

    Did you tried with the documents given by me in the attachment?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    Strange it works with me....

    I'll have a look tomorrow

  5. #5
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Quote Originally Posted by HanneSThEGreaT View Post
    Strange it works with me....
    I am working with visual studio 2008.

    May there be any problem with the webbrowser control?

  6. #6
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Some more things that i pointed out.

    The offline document that i have is composed in winword. Winword uses VML (Vector Markup Language) which converts <img> tags to <v:Imagedata> tags and blocks the working of <img> tag.

    I tried to remove vml formatting from the doucment and got it working.

    Can it be figured out that my webbrowser control can not process vml tags ie. it can not interpret <v:imagedata> as Image.

    Is it possible to do something with vml formatting prior to loading document in web browser control?

    Thanx for any kind of help.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    No ofence, but MS Word is not a program that I'd use to make web pages. I'd use DreamWeaver, MS FrontPage and even Notepad. You are giving yourself too much hassle with doing it through Word. Ms Word puts a lot of unecessary stylesheets and stuff in there. Copy your word's text then Paste it in NotePad

  8. #8
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Quote Originally Posted by HanneSThEGreaT View Post
    No ofence, but MS Word is not a program that I'd use to make web pages. I'd use DreamWeaver, MS FrontPage and even Notepad. You are giving yourself too much hassle with doing it through Word. Ms Word puts a lot of unecessary stylesheets and stuff in there. Copy your word's text then Paste it in NotePad
    True, but i want to make a complete solution, so it is necessary for me to interpret every html document irrespective of the editor in which it is composed.

    There must be some stuff in this regard.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    You using Word is exactly your problem. if you were to do this in NOTEPAD :

    Code:
    <HTML>
    <HEAD>
    <TITLE>IUmages Test - CodeGuru</TITLE>
    </HEAD>
    <BODY>
    
    <p>Image 1</p>
    
    <p><img src="image001.jpg"></p>
    
    <p>Image 2</p>
    
    <p><img src="image002.jpg"></p>
    
    <p>Image 3</p>
    
    <p><img src="image003.jpg"></p>
    
    <p>Image 4</p>
    
    <p><img src="image004.jpg"></p>
    
    <p>Image 5</p>
    
    <p><img src="image005.jpg"></p>
    
    </BODY>
    </HTML>
    And save it as an .html file - your project will work perfectly. As it should. Because you did this webpage in MS Word, it doesn't pick up the SRC attribute of the Images and doesn't pick up the IMG tag. Full stop.
    Listen to what I'm saying, please I'm trying to help you, believe it or not

    I am attching my HTML file here, along with 2 separate ways of looping through the HTML elements, one is easy, as I have shown, the second is very complicated

    BTW, you'll see a folder called "Mine" in both projects - this you'll need to copy to your C Drive, as the web page is in there. For the second ( Complicated project ), there was a reference set to MS HTML Object Library; in case you're wondering....

    Hope it helps...
    Attached Files Attached Files
    • File Type: zip CG.zip (184.5 KB, 209 views)
    Last edited by HanneSThEGreaT; June 15th, 2010 at 05:01 AM.

  10. #10
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Quote Originally Posted by HanneSThEGreaT View Post
    Listen to what I'm saying, please I'm trying to help you, believe it or not
    Respected HanneSThEGreaT
    I believe 100% that you are giving your 100% to help me out.

    I am attching my HTML file here, along with 2 separate ways of looping through the HTML elements, one is easy, as I have shown, the second is very complicated
    Hope it helps...
    I have already tried with both type of methods and resulted in the hard luck.
    I know that my problem is Word and believe me that i am not trying to harras anybody, but this is just my curosity to make it possible with that document too

    Initially the task was over with the normal document, but when the word came in picture, questions were raised and i am just behind it to find the answer..

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    Hello shail24k
    No problems at all, your'e not harrassing anyone LOL!

    OK, if that is the case, the above methods will help us nothing. I'm thinking that we'll need to use a whole different approach. For example, use the TextReader / StreamReader to read the contents of the Word file(s), then try to conjure up something from that

    Tell me this, do you only want to establish how many pictures are there in the file, or do you want to physically make use of those pictures - for example copy them / save them?

    Hannes

  12. #12
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    Quote Originally Posted by HanneSThEGreaT View Post
    Tell me this, do you only want to establish how many pictures are there in the file, or do you want to physically make use of those pictures - for example copy them / save them?
    The complete task is what,
    1. Have to save the images in the folder.
    2. The images will be edited by the designers.
    3. The original images will be replaced with the modified one and then be sent in the form of mail.

    Shail2k4

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    The beauty of MS Word's way of doing things is that it already includes a folder along with the html file. For example, yours is imagetest.html - it generated a folder at the same location named imagetest_files with the pictures in it already. So logically I'd create a program that determines where the html file is, then identify its associated folder and copy the pictures from that folder into your own folder. Does that sound like a plan, or were you looking for something else?

  14. #14
    Join Date
    Jun 2006
    Posts
    148

    Re: Web Browser and Image Elements

    For example, yours is imagetest.html - it generated a folder at the same location named imagetest_files with the pictures in it already
    Ok, that can be done by this way, but when the designer will change the image, he will save it to another location. So need to change the src attribute of the image to locate that path.

  15. #15
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Web Browser and Image Elements

    I had a look at the Word file's format So with simple string manipulation that can be achieved

Page 1 of 2 12 LastLast

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

Featured