2010-04-20 5 views
3

J'ai une page Web qui affiche des contrôles d'éclairage argentés. J'ai besoin de prendre une capture d'écran de cette page Web par programme.Prendre une capture d'écran de la page Web (avec des contrôles Silverlight) par programme

Utilise actuellement le contrôle System.Windows.Forms.WebBrowser pour effectuer une capture d'écran. Forms.WebBrowser fonctionne correctement lorsque je capture des captures d'écran pour des pages normales. Cependant, pour les pages avec des contrôles Silverlight, cela ne fonctionne pas.

Mon code pour prendre la capture d'écran est la suivante: Bitmap bitmap = null; en utilisant (WebBrowser webBrowser = new WebBrowser()) { webBrowser.ScrollBarsEnabled = false; webBrowser.ScriptErrorsSuppressed = true;

  // Set the size of the WebBrowser control 
      webBrowser.Width = width; 
      webBrowser.Height = height; 

      // Load the webpage into a WebBrowser control 
      webBrowser.Navigate(url); 
      while (webBrowser.ReadyState != WebBrowserReadyState.Complete) 
      { 
       Application.DoEvents(); 
      } 

      if (width == -1) 
      { 
       // Take Screenshot of the web pages full width 
       webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width; 
      } 

      if (height == -1) 
      { 
       // Take Screenshot of the web pages full height 
       webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height; 

      } 

      // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control 
      bitmap = new Bitmap(webBrowser.Width, webBrowser.Height); 
      webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser.Width, webBrowser.Height)); 

}

+0

Avez-vous trouvé une réponse? Parce que je suis confronté au même problème. – NLV

Répondre

0

J'utilise ce code pour prendre schreenshot de la page Web:

string myPicsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\SchreenshotFolder\"; 
    if (System.IO.Directory.Exists(myPicsPath)) 
    { 
     Rectangle bounds = yourBrowser.Bounds; 
     using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 
     { 
     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); 
     } 
     bitmap.Save(myPicsPath + System.IO.Path.GetRandomFileName() + ".png", System.Drawing.Imaging.ImageFormat.Png); 
     } 
    } 
    else 
    { 
     System.IO.Directory.CreateDirectory(myPicsPath); 
     MessageBox.Show("Screenshot directory created in " + Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\" + " named SchreenshotFolder\nScreenshot is not saved. Hit button again to save it."); 
    } 

Structure Bounds du contrôle du navigateur Web contient tout ce dont vous avez besoin pour prendre une capture d'écran. Vous pouvez également réduire la taille de l'image bitmap si vous souhaitez l'utiliser comme vignette.

Questions connexes