【问题标题】:Saving an Image from WPF's WebBrowser control - how do you do it?从 WPF 的 WebBrowser 控件保存图像 - 你是怎么做的?
【发布时间】:2010-04-29 06:32:31
【问题描述】:

每个人。可能有一个简单的解决方案,但我似乎找不到。我正在使用 Visual Studio 2010 附带的 WPF 中的 WebBrowser 控件,并尝试以编程方式将可能出现在网页上的图像保存到磁盘。

提前非常感谢! 运气

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    添加System.Drawing作为参考,并在应该捕获图像的方法中执行以下操作:

    Rect bounds = VisualTreeHelper.GetDescendantBounds(browser1);
    
    System.Windows.Point p0 = browser1.PointToScreen(bounds.TopLeft);
    System.Drawing.Point p1 = new System.Drawing.Point((int)p0.X, (int)p0.Y);
    
    Bitmap image = new Bitmap((int)bounds.Width, (int)bounds.Height);
    Graphics imgGraphics = Graphics.FromImage(image);
    
    imgGraphics.CopyFromScreen(p1.X, p1.Y,
                               0, 0,
                               new System.Drawing.Size((int)bounds.Width,
                                                            (int)bounds.Height));
    
    image.Save("C:\\a.bmp", ImageFormat.Bmp);
    

    【讨论】:

    • 经过测试,我发现高分辨率屏幕有问题。在这些情况下,仅捕获了浏览器控件的左上部分。因此,我更改了尺寸计算,如下所示...
    【解决方案2】:

    以下是对@luvieere 解决方案的改编:

    WebBrowser browser1;
    browser1 = this.Browser;
    
    // I used the GetContentBounds()
    Rect bounds = VisualTreeHelper.GetContentBounds(browser1);
    // and the point to screen command for the top-left and the bottom-right corner
    System.Windows.Point pTL = browser1.PointToScreen(bounds.TopLeft);
    System.Windows.Point pBR = browser1.PointToScreen(bounds.BottomRight);
    
    System.Drawing.Bitmap image = new 
    // The size is then calculated as difference of the two corners
    System.Drawing.Bitmap(
        System.Convert.ToInt32(pBR.X - pTL.X),   
        System.Convert.ToInt32(pBR.Y - pTL.Y));
    
    System.Drawing.Graphics imgGraphics = System.Drawing.Graphics.FromImage(image);
    
    imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, new System.Drawing.Size(image.Width, image.Height));
    
    fileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".bmp";
    image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
    

    对于那些喜欢 VB 方言的人

    Dim browser1 As WebBrowser
    browser1 = Me.Browser
    
    Dim bounds As Rect = VisualTreeHelper.GetContentBounds(browser1)
    
    Dim pTL As System.Windows.Point = browser1.PointToScreen(bounds.TopLeft)
    Dim pBR As System.Windows.Point = browser1.PointToScreen(bounds.BottomRight)
    
    Dim image As System.Drawing.Bitmap = New System.Drawing.Bitmap(CInt(pBR.X - pTL.X), CInt(pBR.Y - pTL.Y))
    Dim imgGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(image)
    
    imgGraphics.CopyFromScreen(pTL.X, pTL.Y, 0, 0, New System.Drawing.Size(image.Width, image.Height))
    
    fileName = IO.Path.GetFileNameWithoutExtension(fileName) & ".bmp"
    image.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp)
    

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 2010-09-11
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多