title: Saving a canvas to png C# wpf
body: <p>So I am trying to take a snapshot of my canvas in WPF C# so that I can save it out as a png. The image saves incorrectly at present as it is including the left and top margins.</p>
  
  <p>This is what I have:</p>
  
  <p>create a rectangle for the size of the canvas. if canvas.Margin.Left and Top are set to 0 then the saved image is of the correct size but the offset still occurs and thus cuts the bottom and right edges. Being set the Margin.Left and Top still causes the offset to occur but the whole image is saved but at the wrong size (margin.Left + ActualWidth) rather than just ActualWidth</p>
  
  <pre><code>Rect rect = new Rect(canvas.Margin.Left, canvas.Margin.Top, canvas.ActualWidth, canvas.ActualHeight);
  
  double dpi = 96d;
  
  RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right, (int)rect.Bottom, dpi, dpi, System.Windows.Media.PixelFormats.Default);
  
  rtb.Render(canvas);
  
  BitmapEncoder pngEncoder = new PngBitmapEncoder();
  pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
  
  try
  {
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
  
      pngEncoder.Save(ms);
      ms.Close();
  
      System.IO.File.WriteAllBytes(filename, ms.ToArray());
  }
  catch (Exception err)
  {
      MessageBox.Show(err.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  }
  </code></pre>