title: xBIM Viewer: Create a screenshot from canvas
body: <p>I am using xBim Toolkit (<a href="http://docs.xbim.net/" rel="nofollow noreferrer">http://docs.xbim.net/</a>) for 3D object rendering and there was a requirement to create a screenshot of the current scene on button click.
  xBim is using canvas for graphical representation of the object.
  The entire project is based on C#, .NET tools and Javascript, so this is the range of the tools that can be used for solving this.</p>
  
  <p>I have already tried by using html2canvas (<a href="https://html2canvas.hertzen.com/" rel="nofollow noreferrer">https://html2canvas.hertzen.com/</a>) and dom-to-image (<a href="https://github.com/tsayen/dom-to-image" rel="nofollow noreferrer">https://github.com/tsayen/dom-to-image</a>) libraries, but also tried with default canvas methods (like myCanvas.toDataUrl() or myCanvas.toBlob()).
  All of them produce just an empty image with black or white background, but no xBim object is saved.</p>
  
  <p>The only way I can get any useful screenshot is by using System.Drawing.Graphics and then creating a screenshot of the whole screen (which is not the best implementation for many reasons).</p>
  
  <pre><code>public static void MakeScreenshot()
  {
      var x = Math.Abs(WebBrowser.MousePosition.X);
      var y = Math.Abs(WebBrowser.MousePosition.Y);
      var bounds = Screen.FromPoint(new Point(x, y)).Bounds;
  
      Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
  
      Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
              graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
  
      string screenshotFilePath = Path.Combine(DirectoryPath, "Screenshot.jpg");
  
      bitmap.Save(screenshotFilePath, ImageFormat.Jpeg);
  }
  </code></pre>
  
  <p>I would like to have screenshot of only canvas area.
  It is not important if its implemented by using Javascript or C# code (even with System.Drawing.Graphics, but to cut only part of the screen)</p>