我有一个 C# 应用程序,里面有一个 WebBrowser,该应用程序从论坛中提取被认为是图像的 html 链接,然后我告诉我的 WebBrowser 导航到图像链接以一一显示图像。
问题是其中一些图像比我的 WebBrowser 对象的大小大很多。我不想调整窗口的大小,我喜欢它的大小 :)
我想让 webbrowser 页面调整大小,例如当您打开图像链接并且没有最大化它们时,像 firefox 和 chrome 一样,它们只是使其更小以适应窗口?
提前致谢
解决方案:工作愉快,Elerium(最佳答案)提供了链接,只是将 VB 代码更改为 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());
}