Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2006, 12:00 PM
Authorized User
 
Join Date: May 2004
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default mshtml - how to get images

I am working in a desktop application using .NET/C#. I have written a function which gets a url as an input parameter and matches all opened browser windows with this and returns the html contents
of the document if window found. This is done very successfully. For this purpose i am using ShDocvw.dll and mshtml.dll. The problem is that i want to get images from this (DHTML) document as byte array or as a file whatever is possible. I tried hard to explore it but couldn't yet succeed. Anybody out there to help me. I'll be grately thankful.

        public static string GetIEDocContents(string Url)
        {
            SHDocVw.ShellWindows objWins = new SHDocVw.ShellWindows();
            mshtml.HTMLDocument objDoc;
            string strContents="";
            object refIndex = 1;

            foreach(SHDocVw.InternetExplorer objIE in objWins)
            {
                if(objIE.LocationURL==Url)
                {
                    objDoc = (mshtml.HTMLDocument) objIE.Document;
                    strContents= objDoc.body.parentElement.innerHTML;
                    if(strContents.IndexOf("<FRAME")>=0)
                    {
                        mshtml.FramesCollection objFrames = (mshtml.FramesCollection) objDoc.frames;
                        mshtml.IHTMLWindow2 objFrame = (mshtml.IHTMLWindow2)objFrames.item(ref refIndex);
                        strContents=objFrame.document.body.parentElement.i nnerHTML;
                    }

                    break;
                }
            }

            return strContents;
        }



BaburMan
__________________
BaburMan
 
Old May 24th, 2006, 03:01 AM
Authorized User
 
Join Date: May 2004
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

Well, no repsonse from anywhere , for the time being i have to parse the html i have got for the images i want and got them
from the web using webclient object provided by .NET. Although this is not an efficient solution yet it works and servers the purpose.


thanks



BaburMan
 
Old May 24th, 2006, 06:25 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Will u please send me coding of the function to get containts of particular url's browser window at [email protected]

Bijgupt
 
Old May 26th, 2006, 12:30 PM
Authorized User
 
Join Date: May 2004
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

The code i have posted above works perfectly provided you give a valid url from any of the opened internet explorer window on your machine. However as i mentioned you have to add a reference of microsoft.mshtml.dll and shdocvu.dll to your C# project.




BaburMan
 
Old June 9th, 2006, 06:18 AM
Authorized User
 
Join Date: May 2004
Posts: 73
Thanks: 0
Thanked 1 Time in 1 Post
Default

Successfully got the url source and the images at least. But still i need to find the way to get the css, js and swf. Obviousely css and js script should be grabbed as text and swf as a binary stream or in the form which could be saved to disk. Still waiting from anybody to help or give any link which describes how to accomplish this. The source is as below;

public static string GetIEDocContents(string Url)
{
    SHDocVw.ShellWindows objWins = new SHDocVw.ShellWindows();
    mshtml.HTMLDocument objDoc;
    string strContents="";
    object refIndex = 1;

    foreach(SHDocVw.InternetExplorer objIE in objWins)
    {
        if(objIE.LocationURL==Url)
        {
            objDoc = (mshtml.HTMLDocument) objIE.Document;
            strContents= objDoc.body.parentElement.innerHTML;
            if(strContents.IndexOf("<FRAME")>=0)
            {
                mshtml.FramesCollection objFrames = (mshtml.FramesCollection) objDoc.frames;
                mshtml.IHTMLWindow2 objFrame = (mshtml.IHTMLWindow2)objFrames.item(ref refIndex);
                strContents=objFrame.document.body.parentElement.i nnerHTML;
            }

            string strElName;
            mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2) objDoc.body;
            mshtml.IHTMLControlRange controlRange = (mshtml.IHTMLControlRange) body2.createControlRange();
            mshtml.IHTMLElementCollection objElCol;
            objElCol = objDoc.getElementsByTagName("IMG");

            foreach(mshtml.HTMLImg objImg in objElCol)
            {
                controlRange.add((mshtml.IHTMLControlElement)objIm g);
                controlRange.execCommand("Copy", false, System.Reflection.Missing.Value);
                controlRange.remove(0);
                strElName = objImg.nameProp;

                if (Clipboard.GetDataObject() != null)
                {
                    IDataObject data = Clipboard.GetDataObject();

                    if (data.GetDataPresent(DataFormats.Bitmap))
                    {
                        Image image = (Image)data.GetData(DataFormats.Bitmap,true);
                        if(strElName.Substring(strElName.IndexOf(".")+1).T oLower()=="jpg")
                            image.Save(@"c:\images\"+strElName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        else if(strElName.Substring(strElName.IndexOf(".")+1).T oLower()=="gif")
                            image.Save(@"c:\images\"+strElName, System.Drawing.Imaging.ImageFormat.Gif);
                        else
                            strElName = strElName; //to put a breakpoint and debug to see what's this.
                    }
                    else
                        strElName = strElName;
                }
            }


            break;
        }
    }

    return strContents;
}

BaburMan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
Problems using MSHTML maccas Pro VB 6 2 February 15th, 2006 11:32 AM
How to use MSHTML in Visual Basic 6? raylynn Pro VB 6 1 November 18th, 2005 09:36 AM
mshtml COM script events daveyc Pro VB.NET 2002/2003 0 June 15th, 2005 01:44 PM
Problem using MSHTML maccas Excel VBA 1 November 3rd, 2004 08:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.