Showing posts with label ShDocVw. Show all posts
Showing posts with label ShDocVw. Show all posts

Oct 27, 2008

C# - How to take a screenshot of Internet Explorer

Asuming you have InternetExplorerClass instance, named ie, you can do the following to obtain screenshot:



                  
int screenWidth = ie.Width;
                  
int screenHeight = ie.Height;

                   #region Get IE Bitmap

                  
Bitmap b = new Bitmap(ie.Width, ie.Height);
                  
Graphics g = Graphics.FromImage(b);
                  
IntPtr hdc = g.GetHdc();

                  
bool result = PrintWindow((IntPtr)ie.HWND, hdc, 0);

                   g.ReleaseHdc();
                   g.Flush();
/>                   b.Save(@"filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


You will also need to add the PrintWindow function (it is windows API function):

     [
DllImport("user32.dll")]
    
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt,
        
uint nFlags);

Not that hard, but not well documented. If you don't know how to get a pointer to a running Internet Explorer class, you can do the following:

1. Add reference to ShDocVw (it is in the COM tab and it should be named "Microsoft Objects Automation library" or something like this)
2. Add the following code:

        SHDocVw.
ShellWindowsClass windows = new ShellWindowsClass();

foreach (SHDocVw.InternetExplorer explorer in windows)
        {
          
if (explorer.LocationURL == string.Empty)
           {
            
// this is our guy!
             ie = explorer;
           }
        }
NOTE: please be sure to use your own if statement. This one will get an instance to the last found Internet explorer which has an empty string as Location.


Happy C# programming!

Oct 21, 2008

C# AddIn - Start Debugging and get the running Internet Explorer instance.

I needed to do this (actually I wanted). I was curious if you can obtain an instance of a Internet Explorer which was started in debugging session. Here is a snipet to do this:

        _applicationObject.Debugger.Go(
false);
        SHDocVw.
ShellWindowsClass windows = new ShellWindowsClass();
        
foreach (SHDocVw.InternetExplorer explorer in windows)
        {
          
if (explorer.LocationURL == string.Empty)
           {
            
// this is our guy!
             ie = explorer;
           }
        }


Note : this snipet should be added somewhere where you have access to the _applicationObject.
This is the variable which is declared automatically when you create new instance. I suggest you to put it into the exec method of the AddIn:

     public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
     {
        _applicationObject.Debugger.Go(
false);
        SHDocVw.
ShellWindowsClass windows = new ShellWindowsClass();
        
foreach (SHDocVw.InternetExplorer explorer in windows)
        {
          
if (explorer.LocationURL == string.Empty)
           {
            
// this is our guy!
             ie = explorer;
           }
        }
     }

You also need to add reference to ShDocVw (Microsoft Shell And Automation Library).
Enjoy!

Jun 10, 2007

What happened with the Data mining?

Well, I simply stopped the project for a while, since I need to see the legal aspects of it. I am not quite sure if it is legal to simply take some catalogs from somewhere, reformat them and use them as your own. I think I can do such thing with some free public catalogs, but I will not act, until I have the written permission from the authors. Here are two screenshots of the spider in action :



The above screenshot is when the spider is gathering data. It displays how much data is gathered and what the spider is doing in the given moment.



The above screenshot displays the results of the crawl as a treeview. It uses MS Access database to store the data (since it is a small project).

Technologies used for the spider :
/>.NET 1.1 (Windows Forms), MS Access, Internet Explorer Automation COM Libraries (AxWebBrowser, ShDocVW COM mshtml).