如何消除.png文件中的负空间

时间:2017-06-30 21:02:36

标签: c# wpf

在我的WPF项目中,我有这个类来处理任何给定画布的快照,并将其导出为.PNG。但是,当导出文件时,我会获得大量的空白区域。

enter image description here

这是我的代码:

public static class ExportAsPNG
  {
    public static void SaveCanvas(Window window, Canvas canvas, int dpi, string filename)
    {
      System.Windows.Size size = new System.Windows.Size(window.Width, window.Height);
      canvas.Measure(size);
      //canvas.Arrange(new Rect(size));

      var rtb = new RenderTargetBitmap(
          (int)window.Width, //width 
          (int)window.Height, //height 
          dpi, //dpi x 
          dpi, //dpi y 
          PixelFormats.Pbgra32 // pixelformat 
          );
      rtb.Render(canvas);

      SaveRTBAsPNG(rtb, filename);
    }

    private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
    {
      var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();

      enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
      using (var stm = System.IO.File.Create(filename))
      {
        enc.Save(stm);
      }
    }    
  }

我需要帮助的是理解如何捕获图像而不是整个画布。非常感谢提前!

更新代码

  /// <summary>
  /// Handles the logic for taking a snapshot of any given canvas
  /// </summary>
  public static class ExportImage
  {
    /// <summary>
    /// Gets a snapshot of the canvas and saves it as a bitmap
    /// </summary>
    /// <param name="canvas">the current canvas</param>
    /// <param name="dpi">the desired dpi (96 is default)</param>
    /// <param name="filename">the desired file name</param>
    public static void SaveCanvas(Canvas canvas, int dpi, string filename)
    {
      RenderTargetBitmap rtb = new RenderTargetBitmap(
          (int)Convert.ToInt32(canvas.ActualWidth), //width 
          (int)canvas.ActualHeight, //height 
          dpi, //dpi x 
          dpi, //dpi y 
          PixelFormats.Pbgra32 // pixelformat 
          );
      rtb.Render(canvas);

      SaveRTBAsPNG(rtb, filename);      
    }

    /// <summary>
    /// Takes the target bitmap and converts it to PNG
    /// </summary>
    /// <param name="bmp">the bmp file</param>
    /// <param name="filename">the file name</param>
    private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
    {
      PngBitmapEncoder pngImage = new PngBitmapEncoder();

      pngImage.Frames.Add(BitmapFrame.Create(bmp));
      using (var filestream = System.IO.File.Create(filename))
      {
        pngImage.Save(filestream);
      }
    }    
  }

我的输出

Thisi si what I get after I save my .png

我想要的输出:

This is what I want to get. just the image and not the whole canvas

如您所见,我希望能够保存实际图像

1 个答案:

答案 0 :(得分:1)

您正在请求整个Window大小的位图。相反,你应该确保画布是你想要的尺寸,然后使用画布&#39;高度和宽度:

public static void SaveCanvas(Window window, Canvas canvas, int dpi, string filename)
{
    var rtb = new RenderTargetBitmap(
        (int)canvas.ActualWidth, //width 
        (int)canvas.ActualHeight, //height 
        dpi, //dpi x 
        dpi, //dpi y 
        PixelFormats.Pbgra32 // pixelformat 
        );
    rtb.Render(canvas);

    SaveRTBAsPNG(rtb, filename);
}