画布转换为WPF的WriteableBitmap的最快方法是什么? [英] The fastest way to convert canvas to the writeablebitmap in WPF?

查看:809
本文介绍了画布转换为WPF的WriteableBitmap的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个WriteableBitmap的图像和画布与图纸和我想的图像发送到peer.In为了减少带宽,我想画布转换为WriteableBitmap的,因此,我可以的blit两个图像到新的WriteableBitmap的。问题是我无法找到到画布转换的好方法。
所以,我想问一下,如果有画布到WriteableBitmap的类转换直接的方式。

I currently have one writeablebitmap image and canvas with drawings and I want to send the images to the peer.In order to reduce the bandwidth, I would like to convert canvas to the writeablebitmap, thus I can blit both the images to a new writeablebitmap. The problem is I cannot find a good way to convert the canvas. Therefore, I would like to ask if there is a direct way to convert the canvas to a writeablebitmap class.

推荐答案

这是从的这个博客帖子但不是写入文件,将其写入到一个WriteableBitmap的。

This is taken from this blog post but instead of writing to a file, it writes to a 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);

}

这篇关于画布转换为WPF的WriteableBitmap的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆