experchange > dotnet.framework.* > dotnet.framework.windowsforms

Peter (09-18-07, 10:16 AM)
Hi,
we're using WebbrowserControl to show different documents like pictures,
movies, word-Docs etc.
Showing pictures in big resolution like 3048*2000 the webbrowser control
shows only a small part of the pic (size of the webbrowser control).
The IE has an option to resize pictures to visible browser. How can I
perform this with the webbrowser Control?
To resize the pics before showing isn't a good idea - or is it the only way?

Thanks !
Peter
Bob Powell [MVP] (09-18-07, 11:18 AM)
Rather than place the image directly into the web-browser, use an HTML page
defined as a string with the image in it. That way you can control the size
of the image using HTML.

To do this you can implement IStream and IDocHostUIHandler and create
little pages either from stock text or on the fly.

/// <summary>
/// Implements the IStream interface on a memory stream
/// </summary>
class Streamer : IPersistStreamInitInterop.IStream
{
MemoryStream ms = new MemoryStream();
StreamWriter sw;
/// <summary>
/// Constructs the Streamer object
/// </summary>
public Streamer()
{
sw=new StreamWriter(ms);
}
/// <summary>
/// Writes a string to the memory stream
/// </summary>
/// <param name="s">The string to write</param>
public void Write(string s)
{
sw.Write(s);
sw.Flush();
ms.Seek(0,SeekOrigin.Begin);
}
#region Implementation of IStream
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
/// <param name="grfCommitFlags"></param>
public void Commit(uint grfCommitFlags)
{
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void Clone(out IPersistStreamInitInterop.IStream ppstm)
{
ppstm = this;
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void LockRegion(IPersistStreamInitInterop._ULARGE_INTEG ER libOffset,
IPersistStreamInitInterop._ULARGE_INTEGER cb, uint dwLockType)
{
}
/// <summary>
/// Reads one byte from the stream
/// </summary>
/// <param name="pv">A reference to the destination byte</param>
/// <param name="cb">the count, ignoted if more than 1</param>
/// <param name="pcbRead">returns the count of bytes read</param>
public void RemoteRead(out byte pv, uint cb, out uint pcbRead)
{
if(ms.Position<ms.Length-1)
{
pv=(byte)ms.ReadByte();
pcbRead=(uint)1;
}
else
{
pv=0;
pcbRead=0;
}
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void Revert()
{
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void RemoteCopyTo(IPersistStreamInitInterop.IStream pstm,
IPersistStreamInitInterop._ULARGE_INTEGER cb, out
IPersistStreamInitInterop._ULARGE_INTEGER pcbRead, out
IPersistStreamInitInterop._ULARGE_INTEGER pcbWritten)
{
pcbRead = new IPersistStreamInitInterop._ULARGE_INTEGER();
pcbWritten = new IPersistStreamInitInterop._ULARGE_INTEGER();
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void RemoteSeek(IPersistStreamInitInterop._LARGE_INTEGE R dlibMove,
uint dwOrigin, out IPersistStreamInitInterop._ULARGE_INTEGER
plibNewPosition)
{
plibNewPosition = new IPersistStreamInitInterop._ULARGE_INTEGER();
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void UnlockRegion(IPersistStreamInitInterop._ULARGE_INT EGER
libOffset, IPersistStreamInitInterop._ULARGE_INTEGER cb, uint dwLockType)
{
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void SetSize(IPersistStreamInitInterop._ULARGE_INTEGER libNewSize)
{
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void Stat(out IPersistStreamInitInterop.tagSTATSTG pstatstg, uint
grfStatFlag)
{
pstatstg = new IPersistStreamInitInterop.tagSTATSTG();
}
/// <summary>
/// Not used by IPersistStreamInit this is just a stub
/// </summary>
public void RemoteWrite(ref byte pv, uint cb, out uint pcbWritten)
{
pcbWritten = 0;
}
#endregion
}
Then you can implement MsHtmHstInterop.IDocHostUIHandler in the following
manner...

class host : Control, MsHtmHstInterop.IDocHostUIHandler
{
string _defaultpage=
@"<html>
<head>
<title></title>+
</head>+
<BODY bgcolor=""#{0:X2}{1:X2}{2:X2}"" Text=""#cccccc"" leftmargin=""0""
topmargin=""0"" rightmargin=""0"" bottommargin=""0"" scroll=""no"">
</BODY>
</html>
";
string _imageWrapper=
@"<html>
<head>
<title></title>
</head>
<BODY bgcolor=""#000000"" Text=""#cccccc"" leftmargin=""0"" topmargin=""0""
rightmargin=""0"" bottommargin=""0"" scroll=""no"">
<p><img src=""{0}""/></p>
</BODY>
</html>
";
private AxSHDocVw.AxWebBrowser axWebBrowser1;
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden)
]
public string ImageWrapper
{
get{return _imageWrapper;}
set{_imageWrapper=value;}
}
public string DefaultUrl
{
get{return _defaultUrl;}
set
{
_defaultUrl=value;
}
}
/// <summary>
/// Used to set and navigate to an image file locally on disc. The filename
will be substituted for the "{0}" formatting code in the SWFWrapper property
and this complete web-page sent to the browser internally
/// </summary>
[Browsable(false)]
public string ImagePath
{
get{return _imagePath;}
set
{
this._imagePath=value;
if(_killBrowser)
NewBrowser();
if(value== null || value=="")
return;
Streamer sw = new Streamer();
sw.Write(string.Format(_imageWrapper,value,this.Wi dth,this.Height));
IHTMLDocument2 doc;
if(this.axWebBrowser1.Document==null || this.axWebBrowser1.Document as
IHTMLDocument2==null)
{
System.Diagnostics.Trace.WriteLine("Doc was null??!!");
this.NavigateDefault(true);
}
doc=(IHTMLDocument2)this.axWebBrowser1.Document;
IPersistStreamInit psi=(IPersistStreamInit)doc;
psi.InitNew();
psi.Load((IPersistStreamInitInterop.IStream)sw);
}
}
public AxSHDocVw.AxWebBrowser Browser
{
get{return this.axWebBrowser1;}
}
[Description("Navigates to the default page and resets the UI handler")]
public void NavigateDefault(bool resetui)
{
NavigateDefault();
if(resetui)
this.ResetUIHandler();
}
bool _killBrowser=false;
public void NewBrowser()
{
_killBrowser=false;
if(axWebBrowser1!=null)
{
this.Controls.Remove(axWebBrowser1);
axWebBrowser1.Dispose();
}
this.SuspendLayout();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.a xWebBrowser1)).BeginInit();
//this.axWebBrowser1.BeginInit();
if(this.DesignMode==true)
{
axWebBrowser1.Location=new Point(5,5);
axWebBrowser1.Size=new
Size(this.ClientSize.Width-10,this.ClientSize.Height-10);
}
else
{
axWebBrowser1.Location=new Point(0,0);
axWebBrowser1.Size=this.ClientSize;
}
ResXResourceReader rr=new
ResXResourceReader(this.GetType().Assembly.GetMani festResourceStream("ExplorerHost.TextFile1.txt"));
IDictionaryEnumerator de = rr.GetEnumerator();
object o = null;
while(de.MoveNext())
{
string s = (string)de.Key;
if(s=="axWebBrowser1.OcxState")
o=de.Value;
}
//
// axWebBrowser1
//
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)o);
this.axWebBrowser1.TabIndex = 0;
this.axWebBrowser1.Visible=true;
// axWebBrowser1.DownloadBegin+=new EventHandler(DownloadBegin);
//
// ExplorerHost
//
this.Controls.Add(axWebBrowser1);
//this.axWebBrowser1.EndInit();
((System.ComponentModel.ISupportInitialize)(axWebB rowser1)).EndInit();
this.ResumeLayout(false);
}
public void Stop()
{
object o="";
object p="";
object q="";
object r="";
object url = "about:blank";
this.axWebBrowser1.Navigate2(ref url,ref o,ref p,ref q,ref r);
}
/// <summary>
/// Internal private method to navigate to the default URL
/// </summary>
[Description("Navigates to the default page")]
public void NavigateDefault()
{
if(DefaultUrl==null)
{
Streamer sw = new Streamer();
sw.Write(string.Format(_defaultpage,this.BackColor .R,this.BackColor.G,this.BackColor.B));
IHTMLDocument2 doc;
int count=0;
while(this.axWebBrowser1.Document as IHTMLDocument2 == null)
{
if(count>0)
NewBrowser();
count++;
System.Diagnostics.Trace.WriteLine("Doc was null??!!");
object o="";
object p="";
object q="";
object r="";
object url = "about:blank";
this.axWebBrowser1.Navigate2(ref url,ref o,ref p,ref q,ref r);
}
doc=(IHTMLDocument2)this.axWebBrowser1.Document;
IPersistStreamInit psi=(IPersistStreamInit)doc;
psi.InitNew();
psi.Load((IPersistStreamInitInterop.IStream)sw);
}
else
{
object flags=0;
object targetframe=string.Empty;
object postdata=string.Empty;
object headers=string.Empty;
object url = this.DefaultUrl;
this.Browser.Navigate2(ref url, ref flags, ref targetframe, ref postdata,
ref headers);
}
}
public ExplorerHost()
{
this.NewBrowser();
this.NavigateDefault(true);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
/// <summary>
/// Handles the DownloadBegin event from the internal browser control to
ensure that the IDocHostUIHandler is always up to date
/// </summary>
/// <param name="sender">The object that initiated the event, usually the
internal browser control</param>
/// <param name="e">Dummy event arguments</param>
void DownloadBegin(object sender, EventArgs e)
{
//this.ResetUIHandler();
}

#region Implementation of IDocHostUIHandler
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="fEnable"></param>
public void EnableModeless(int fEnable)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="pchKey"></param>
/// <param name="dw"></param>
public void GetOptionKeyPath(out string pchKey, uint dw)
{
pchKey = null;
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="lpmsg"></param>
/// <param name="pguidCmdGroup"></param>
/// <param name="nCmdID"></param>
public void TranslateAccelerator(ref MsHtmHstInterop.tagMSG lpmsg, ref
System.Guid pguidCmdGroup, uint nCmdID)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="pDO"></param>
/// <param name="ppDORet"></param>
public void FilterDataObject(MsHtmHstInterop.IDataObject pDO, out
MsHtmHstInterop.IDataObject ppDORet)
{
ppDORet = null;
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="fActivate"></param>
public void OnFrameWindowActivate(int fActivate)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
public void UpdateUI()
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="dwID"></param>
/// <param name="ppt"></param>
/// <param name="pcmdtReserved"></param>
/// <param name="pdispReserved"></param>
public void ShowContextMenu(uint dwID, ref MsHtmHstInterop.tagPOINT ppt,
object pcmdtReserved, object pdispReserved)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="dwTranslate"></param>
/// <param name="pchURLIn"></param>
/// <param name="ppchURLOut"></param>
public void TranslateUrl(uint dwTranslate, ref short pchURLIn, System.IntPtr
ppchURLOut)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="dwID"></param>
/// <param name="pActiveObject"></param>
/// <param name="pCommandTarget"></param>
/// <param name="pFrame"></param>
/// <param name="pDoc"></param>
public void ShowUI(uint dwID, MsHtmHstInterop.IOleInPlaceActiveObject
pActiveObject, MsHtmHstInterop.IOleCommandTarget pCommandTarget,
MsHtmHstInterop.IOleInPlaceFrame pFrame, MsHtmHstInterop.IOleInPlaceUIWindow
pDoc)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="ppDispatch"></param>
public void GetExternal(out object ppDispatch)
{
ppDispatch = null;
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="prcBorder"></param>
/// <param name="pUIWindow"></param>
/// <param name="fRameWindow"></param>
public void ResizeBorder(ref MsHtmHstInterop.tagRECT prcBorder,
MsHtmHstInterop.IOleInPlaceUIWindow pUIWindow, int fRameWindow)
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="pDropTarget"></param>
/// <param name="ppDropTarget"></param>
public void GetDropTarget(MsHtmHstInterop.IDropTarget pDropTarget, out
MsHtmHstInterop.IDropTarget ppDropTarget)
{
ppDropTarget = null;
}
/// <summary>
/// Sets or resets the host info flags according to the property settings in
this control.
/// </summary>
/// <param name="pInfo"></param>
public void GetHostInfo(ref MsHtmHstInterop._DOCHOSTUIINFO pInfo)
{
if(this._hide3DBorder)
{
pInfo.dwFlags|=(uint)MsHtmHstInterop.tagDOCHOSTUIF LAG.DOCHOSTUIFLAG_NO3DBORDER;
pInfo.dwFlags|=(uint)0x00200000;//no3DouterBorder
}
if(this._hideScrollbars)
pInfo.dwFlags|=(uint)MsHtmHstInterop.tagDOCHOSTUIF LAG.DOCHOSTUIFLAG_SCROLL_NO;
}
/// <summary>
/// Not currently implemented
/// </summary>
public void HideUI()
{
}
/// <summary>
/// Not currently implemented
/// </summary>
/// <param name="fActivate"></param>
public void OnDocWindowActivate(int fActivate)
{
}
#endregion
/// <summary>
/// Resized the internal web-browser control according to the requirements
of the wrapper.
/// </summary>
/// <remarks>
/// In design mode, the internal browser is sized to allow a 5 pixel gap
around the whole control so that the wrapper control may be sized.
/// At run time, the internal browser is sized to cover the whole wrapper
control.
/// </remarks>
/// <param name="e"></param>
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if(this.DesignMode)
{
this.axWebBrowser1.Location=new Point(5,5);
this.axWebBrowser1.Size=new
Size(this.ClientSize.Width-10,this.ClientSize.Height-10);
}
else
{
this.axWebBrowser1.Location=new Point(0,0);
this.axWebBrowser1.Size=this.ClientSize;
}
}
protected override void OnBackColorChanged(EventArgs e)
{
base.OnBackColorChanged(e);
if(this._defaultUrl==null)
this.NavigateDefault();
}
/// <summary>
/// Internal method that informs the internal browser that this wrapper
control provides the IDocHostUIHandler implementation.
/// </summary>
void ResetUIHandler()
{
if(this.axWebBrowser1.Document==null)
{
this.NavigateDefault(false);
}
ICustomDoc cd=(ICustomDoc)this.axWebBrowser1.Document;
cd.SetUIHandler((IDocHostUIHandler)this);
}
}

--
Peter (09-18-07, 12:06 PM)
Hi Bob,
thanks for your excellent reply - i forgot to say, we're using vb.net 2005.
But i'll try this and convert it to a vb-code as far as possible. But if you
have this in vb also, i would be glad, if you can post it too.

Thanks a lot!
Peter

"Bob Powell [MVP]" wrote:
[..]
mblaney853 (04-30-19, 01:28 AM)
On Tuesday, September 18, 2007 at 4:16:08 AM UTC-4, Peter wrote:
[..]
Similar Threads