【问题标题】:How to take WPF Screenshot with WebPage embedded如何获取嵌入网页的 WPF 屏幕截图
【发布时间】:2012-09-26 02:03:05
【问题描述】:

我有一个 WPF 应用程序。该应用程序中嵌入了 2 个网页。当我截取屏幕截图时,我得到了除嵌入式网页之外的整个应用程序。

我需要截图,包括网页。有人可以指导我吗..

谢谢

【问题讨论】:

    标签: c# wpf wpf-controls webpage-screenshot


    【解决方案1】:
    try
        {
          // System.Drawing.Point p=new System.Drawing.Point(100,500);
            Bitmap b = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics=Graphics.FromImage(b as Image);
            graphics.CopyFromScreen(0,0,0,0,b.Size);
    
           string stringsFile=@"C:\Image1";
    
            b.Save(stringsFile,ImageFormat.Png);
         }
      catch (Exception exp)
          {
          Microsoft.Windows.Controls.MessageBox.Show("Opps !!! " + exp.Message);
          } 
    

    【讨论】:

      【解决方案2】:

      你可以试试这段代码

      /// Gets a JPG "screenshot" of the current UIElement
          ///
          /// UIElement to screenshot
          /// Scale to render the screenshot
          /// JPG Quality
          /// Byte array of JPG data
          public static byte[] GetJpgImage(this UIElement source, double scale, int quality)
          {
              double actualHeight = source.RenderSize.Height;
              double actualWidth = source.RenderSize.Width;
      
              double renderHeight = actualHeight * scale;
              double renderWidth = actualWidth * scale;
      
              RenderTargetBitmap renderTarget = new RenderTargetBitmap((int) renderWidth, (int) renderHeight, 96, 96, PixelFormats.Pbgra32);
              VisualBrush sourceBrush = new VisualBrush(source);
      
              DrawingVisual drawingVisual = new DrawingVisual();
              DrawingContext drawingContext = drawingVisual.RenderOpen();
      
              using (drawingContext)
              {
                  drawingContext.PushTransform(new ScaleTransform(scale, scale));
                  drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
              }
              renderTarget.Render(drawingVisual);
      
              JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
              jpgEncoder.QualityLevel = quality;
              jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
      
              Byte[] _imageArray;
      
              using (MemoryStream outputStream = new MemoryStream())
              {
                  jpgEncoder.Save(outputStream);
                  _imageArray = outputStream.ToArray();
              }
      
              return _imageArray;
          }
      

      【讨论】:

      • 这不起作用,屏幕的网页部分显示为空白。
      猜你喜欢
      • 2010-10-16
      • 1970-01-01
      • 2012-06-16
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多