【问题标题】:The fastest way to convert canvas to the writeablebitmap in WPF?将画布转换为 WPF 中可写位图的最快方法?
【发布时间】:2013-04-17 02:03:39
【问题描述】:

我目前有一个可写位图图像和带有绘图的画布,我想将图像发送给对等方。为了减少带宽,我想将画布转换为可写位图,因此我可以将两个图像都blit到一个新的可写位图。问题是我找不到转换画布的好方法。 所以想问问有没有直接的办法可以把canvas转成writeablebitmap类。

【问题讨论】:

  • 我在 WPF 中使用 C#。

标签: c# wpf writeablebitmap


【解决方案1】:

这取自 this blog post,但它不是写入文件,而是写入 WriteableBitmap。

public WriteableBitmap SaveAsWriteableBitmap(Canvas surface)
{
    if (surface == null) return null;

    // Save current canvas transform
    Transform transform = surface.LayoutTransform;
    // reset current transform (in case it is scaled or rotated)
    surface.LayoutTransform = null;

    // Get the size of canvas
    Size size = new Size(surface.ActualWidth, surface.ActualHeight);
    // Measure and arrange the surface
    // VERY IMPORTANT
    surface.Measure(size);
    surface.Arrange(new Rect(size));

    // Create a render bitmap and push the surface to it
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
      (int)size.Width, 
      (int)size.Height, 
      96d, 
      96d, 
      PixelFormats.Pbgra32);
    renderBitmap.Render(surface);


    //Restore previously saved layout
    surface.LayoutTransform = transform;

    //create and return a new WriteableBitmap using the RenderTargetBitmap
    return new WriteableBitmap(renderBitmap);

}

【讨论】:

    猜你喜欢
    • 2011-10-14
    • 2015-08-23
    • 1970-01-01
    • 2011-04-05
    • 2012-05-11
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多