Reputation: 652
I've got a C# application, with a WebBrowser inside, the application pulls html links that are believed to be images from a forum and I then tell my WebBrowser to navigate to the image link to display the images one by one.
The problem is that some of these images are a lot larger than the size of my WebBrowser object. I DO NOT WANT TO RESIZE THE WINDOW, I LIKE IT THE SIZE IT IS :)
I want to get the webbrowser page to resize, for example like firefox and chrome do when you open an image link and you dont have them maximized, they just make it smaller to fit the window?
Thanks in advance
SOLUTION: Works a treat, Elerium(Top Answer) provided the link, just changed the VB code to C#
double origSizeWidth, origSizeHeight;
origSizeWidth =webBrowser1.Size.Width; //get the original size of browser
origSizeHeight = webBrowser1.Size.Height;
HtmlElement pic = webBrowser1.Document.Images[0]; //get your image
double origPicWidth, origPicHeight;
origPicWidth = double.Parse(pic.GetAttribute("WIDTH")); //save the image width
origPicHeight = double.Parse(pic.GetAttribute("HEIGHT"));//and height
double tempW, tempY, widthScale, heightScale;
double scale = 0;
if(origPicWidth > origSizeWidth){
widthScale = origSizeWidth / origPicWidth; //find out the scale factor for the width
heightScale = origSizeHeight / origPicHeight; //scale factor for height
scale = Math.Min(widthScale,heightScale);//determine which scale to use from the smallest
tempW = origPicWidth * scale; //multiply picture original width by the scale
tempY = origPicHeight * scale; // multiply original picture height by the scale
pic.SetAttribute("WIDTH", tempW.ToString()); // set your attributes
pic.SetAttribute("HEIGHT", tempY.ToString());
}
Upvotes: 2
Views: 10567