Click here to Skip to main content
15,936,337 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I used WebBrowser component from Toolbox to my Form in C#.
I have decided to reduce memory usage and I need to take its images off.
I found no function to do that in: System.Windows.Forms.WebBrowser
Can anyone help me?

John Simmons / outlaw programmer wrote:
Are you allowing the user to browse, or are you providing the content programatically?


This program has two modes.
Regarding to its adjusted options, the user may see the webbrowser images or not. But, he/she can always see the webbrowser and the location that it is navigated in.

Christian Graus wrote:

I can't imagine either wanting to browse the web with no images, or having a computer with such a small amount of memory that I couldn't handle the images on the average website. I think you're chasing something totally unnecessary. However, the most basic[^] web search gives tons of info, so I guess there's enough of a reason that plenty of people are trying to do it.

I am running a BOT that runs a series of instructions on a webbrowser and the [professional] user have to know what the BOT is doing. On the other hand, this BOT may be run on a dedicated server and there is RAM and CPU usage limitation. In addition, running many instances of this program worsens the problem. Therefore the thing I am looking for is absolutely necessary. There are many websites but none of them worked. I'm looking for a person that have done such a work before.



:rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose:
:rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose:
:rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose:
:rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose::rose:

Thank you
I solved the problem by modifying an example to a library that makes its usage easy. Just main class has to be derived from myForm instead of Form and also attribution [ComVisible(true)] has to be added before it. The function BrowserLoadImages helps the programmer to get permission to or prevent browser from loading images. It also is extendable.

using library::thumbsdown:
...
    [ComVisible(true)]
    public partial class Form1 : myFormSpace.myForm
    {
          ...
          BrowserLoadImages(this.webBrowser1, checkBox_DownloadImages.Checked);
          ...


library code::thumbsdown:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace myFormSpace
{
    public enum BrowserOptions : uint
    {
        /// <summary>
        /// No flags are set.
        /// </summary>
        None = 0,
        /// <summary>
        /// The browser will operate in offline mode. Equivalent to DLCTL_FORCEOFFLINE.
        /// </summary>
        AlwaysOffline = 0x10000000,
        /// <summary>
        /// The browser will play background sounds. Equivalent to DLCTL_BGSOUNDS.
        /// </summary>
        BackgroundSounds = 0x00000040,
        /// <summary>
        /// Specifies that the browser will not run Active-X controls. Use this setting
        /// to disable Flash movies. Equivalent to DLCTL_NO_RUNACTIVEXCTLS.
        /// </summary>
        DontRunActiveX = 0x00000200,
        /// <summary>
        ///  Specifies that the browser should fetch the content from the server. If the server's
        ///  content is the same as the cache, the cache is used.Equivalent to DLCTL_RESYNCHRONIZE.
        /// </summary>
        IgnoreCache = 0x00002000,
        /// <summary>
        ///  The browser will force the request from the server, and ignore the proxy, even if the
        ///  proxy indicates the content is up to date.Equivalent to DLCTL_PRAGMA_NO_CACHE.
        /// </summary>
        IgnoreProxy = 0x00004000,
        /// <summary>
        ///  Specifies that the browser should download and display images. This is set by default.
        ///  Equivalent to DLCTL_DLIMAGES.
        /// </summary>
        Images = 0x00000010,
        /// <summary>
        ///  Disables downloading and installing of Active-X controls.Equivalent to DLCTL_NO_DLACTIVEXCTLS.
        /// </summary>
        NoActiveXDownload = 0x00000400,
        /// <summary>
        ///  Disables web behaviours.Equivalent to DLCTL_NO_BEHAVIORS.
        /// </summary>
        NoBehaviours = 0x00008000,
        /// <summary>
        ///  The browser suppresses any HTML charset specified.Equivalent to DLCTL_NO_METACHARSET.
        /// </summary>
        NoCharSets = 0x00010000,
        /// <summary>
        ///  Indicates the browser will ignore client pulls.Equivalent to DLCTL_NO_CLIENTPULL.
        /// </summary>
        NoClientPull = 0x20000000,
        /// <summary>
        ///  The browser will not download or display Java applets.Equivalent to DLCTL_NO_JAVA.
        /// </summary>
        NoJava = 0x00000100,
        /// <summary>
        ///  The browser will download framesets and parse them, but will not download the frames
        ///  contained inside those framesets.Equivalent to DLCTL_NO_FRAMEDOWNLOAD.
        /// </summary>
        NoFrameDownload = 0x00080000,
        /// <summary>
        ///  The browser will not execute any scripts.Equivalent to DLCTL_NO_SCRIPTS.
        /// </summary>
        NoScripts = 0x00000080,
        /// <summary>
        ///  If the browser cannot detect any internet connection, this causes it to default to
        ///  offline mode.Equivalent to DLCTL_OFFLINEIFNOTCONNECTED.
        /// </summary>
        OfflineIfNotConnected = 0x80000000,
        /// <summary>
        ///  Specifies that UTF8 should be used.Equivalent to DLCTL_URL_ENCODING_ENABLE_UTF8.
        /// </summary>
        UTF8 = 0x00040000,
        /// <summary>
        ///  The browser will download and display video media.Equivalent to DLCTL_VIDEOS.
        /// </summary>
        Videos = 0x00000020
    }


    #region COM Interfaces
    [StructLayout(LayoutKind.Sequential)]
    internal struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }




    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public int message;
        public IntPtr wParam;
        public IntPtr lParam;
        public int time;
        public int pt_x;
        public int pt_y;
    }



    [ComVisible(true), Guid("0000011B-0000-0000-C000-000000000046"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleContainer
    {
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] Object pbc,
            [In, MarshalAs(UnmanagedType.LPWStr)] String pszDisplayName, [Out,
            MarshalAs(UnmanagedType.LPArray)] int[] pchEaten, [Out,
            MarshalAs(UnmanagedType.LPArray)] Object[] ppmkOut);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int EnumObjects([In, MarshalAs(UnmanagedType.U4)] uint grfFlags, [Out,
            MarshalAs(UnmanagedType.LPArray)] Object[] ppenum);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int LockContainer([In, MarshalAs(UnmanagedType.Bool)] Boolean fLock);
    }

    [ComVisible(true), Guid("00000118-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleClientSite
    {
        [PreserveSig]
        int SaveObject();
        [PreserveSig]
        int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
        [PreserveSig]
        int GetContainer(out object container);
        [PreserveSig]
        int ShowObject();
        [PreserveSig]
        int OnShowWindow(int fShow);
        [PreserveSig]
        int RequestNewObjectLayout();
    }

    [ComVisible(true), ComImport(),
    Guid("00000112-0000-0000-C000-000000000046"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleObject
    {
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetClientSite([In, MarshalAs(UnmanagedType.Interface)] IOleClientSite
            pClientSite);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetClientSite([Out, MarshalAs(UnmanagedType.Interface)] out IOleClientSite site);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetHostNames([In, MarshalAs(UnmanagedType.LPWStr)] String
            szContainerApp, [In, MarshalAs(UnmanagedType.LPWStr)] String
            szContainerObj);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Close([In, MarshalAs(UnmanagedType.U4)] uint dwSaveOption);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetMoniker([In, MarshalAs(UnmanagedType.U4)] uint dwWhichMoniker, [In,
            MarshalAs(UnmanagedType.Interface)] Object pmk);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetMoniker([In, MarshalAs(UnmanagedType.U4)] uint dwAssign, [In,
            MarshalAs(UnmanagedType.U4)] uint dwWhichMoniker, [Out, MarshalAs(UnmanagedType.Interface)] out Object moniker);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int InitFromData([In, MarshalAs(UnmanagedType.Interface)] Object
            pDataObject, [In, MarshalAs(UnmanagedType.Bool)] Boolean fCreation, [In,
            MarshalAs(UnmanagedType.U4)] uint dwReserved);
        int GetClipboardData([In, MarshalAs(UnmanagedType.U4)] uint dwReserved, out
			Object data);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int DoVerb([In, MarshalAs(UnmanagedType.I4)] int iVerb, [In] IntPtr lpmsg,
            [In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pActiveSite, [In,
            MarshalAs(UnmanagedType.I4)] int lindex, [In] IntPtr hwndParent, [In] RECT
            lprcPosRect);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int EnumVerbs(out Object e); // IEnumOLEVERB
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int OleUpdate();
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int IsUpToDate();
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetUserClassID([In, Out] ref Guid pClsid);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetUserType([In, MarshalAs(UnmanagedType.U4)] uint dwFormOfType, [Out,
            MarshalAs(UnmanagedType.LPWStr)] out String userType);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetExtent([In, MarshalAs(UnmanagedType.U4)] uint dwDrawAspect, [In]
			Object pSizel); // tagSIZEL
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetExtent([In, MarshalAs(UnmanagedType.U4)] uint dwDrawAspect, [Out]
			Object pSizel); // tagSIZEL
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Advise([In, MarshalAs(UnmanagedType.Interface)] System.Runtime.InteropServices.ComTypes.IAdviseSink pAdvSink, out
			int cookie);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Unadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int EnumAdvise(out Object e);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int GetMiscStatus([In, MarshalAs(UnmanagedType.U4)] uint dwAspect, out int
            misc);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int SetColorScheme([In] Object pLogpal); // tagLOGPALETTE
    }


    [ComImport, Guid("B196B288-BAB4-101A-B69C-00AA00341D07"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleControl
    {
        [PreserveSig]
        int GetControlInfo([Out] object pCI);
        [PreserveSig]
        int OnMnemonic([In] ref MSG pMsg);
        [PreserveSig]
        int OnAmbientPropertyChange(int dispID);
        [PreserveSig]
        int FreezeEvents(int bFreeze);
    }

    #endregion COM Interfaces

    [ComVisible(true)]
    public class myForm : Form, myFormSpace.IOleClientSite
    {
        public BrowserOptions webBrowserOptions = BrowserOptions.DontRunActiveX | BrowserOptions.NoClientPull | BrowserOptions.NoJava | BrowserOptions.NoActiveXDownload;

        [DispId(-5512)]
        public virtual int IDispatch_Invoke_Handler()
        {
            System.Diagnostics.Debug.WriteLine("-5512");
            return (int)webBrowserOptions;
        }
        #region IOleClientSite Members

        public int SaveObject()
        {
            return 0;
        }

        public int GetMoniker(int dwAssign, int dwWhichMoniker, out object moniker)
        {
            moniker = this;
            return 0;
        }

        public int GetContainer(out object container)
        {
            container = this;
            return 0;
        }

        public int ShowObject()
        {
            return 0;
        }

        public int OnShowWindow(int fShow)
        {
            return 0;
        }

        public int RequestNewObjectLayout()
        {
            return 0;
        }

        #endregion
        public void BrowserLoadImages(WebBrowser wBrowser,bool showImages)
        {
            if (showImages)
                webBrowserOptions |= BrowserOptions.Images;
            else
                webBrowserOptions &= ~BrowserOptions.Images;
            IOleControl obj = (IOleControl)wBrowser.ActiveXInstance;
            //notify browser of change
            obj.OnAmbientPropertyChange(-5512);
        }
    }
}
Posted
Updated 17-Mar-10 1:31am
v7

You've posted a bit here, you should know by now that the 'answer' button is for answers, not for random comments. Edit your post or use the forum at the bottom to comment on your post, unless you're posting the answer.

I can't imagine either wanting to browse the web with no images, or having a computer with such a small amount of memory that I couldn't handle the images on the average website. I think you're chasing something totally unnecessary. However, the most basic[^] web search gives tons of info, so I guess there's enough of a reason that plenty of people are trying to do it.

In response to your reply: the link I gave you has tons of examples of code to do what you want. I still think it's a rare enough task that you'll be lucky to find someone who has done it, and that if you have a ton of links with code, you should start there and work out how to do what you want.

Just to add, giving me a one vote for giving you a right answer and some advice on how to use this forum makes you a retard.
 
Share this answer
 
v3
Comments
Simon Bang Terkildsen 24-Aug-11 6:46am    
+5
Are you allowing the user to browse, or are you providing the content programatically?

I found the following when I Googled "C# webbrowser prevent images":

http://slingkid.blogsome.com/2006/05/26/blocking-images-similar-to-outlook/[^]
 
Share this answer
 
v3
Thanks for the code, it is what I needed. I'm using WPF though and I can't inherit my form from your class, but I found out that it is not needed to be a Form, just any class with the attribute ComVisible=true, so I added this function to your class:

[ComVisible(true)]
public class WebBrowserBehaviorModifier : IOleClientSite
{
    WebBrowser contentBrowser = null;

    public void InitBrowser(WebBrowser b)
    {
        contentBrowser = b;

        IOleObject obj = (IOleObject)contentBrowser.ActiveXInstance;
        obj.SetClientSite(this);

    }
    ...

(it could be the constructor instead of a function for a safer usage)

Also you didn't mention that it is important to change the next line in the AssemblyInfo.cs:

[assembly: ComVisible(true)]


otherwise the attribute of the class won't have any effect
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900