从 XAML 视图生成图像

发布于 2024-10-28 00:03:02 字数 125 浏览 3 评论 0原文

我想动态生成一些图像。为此,我打算创建一个 XAML 视图,用数据填充它(使用 DataBinding),然后从该视图的渲染中生成图像(类似于屏幕截图)。

有没有办法在 Silverlight 或 WPF 中做到这一点?

I would like to generate some images dynamicaly. For that, I intend to create a XAML View, populate it with Data (using DataBinding) and then generate an image from the rendering of that view (kind of a screenshot).

Is there a way to do this in Silverligth or WPF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-11-04 00:03:02

在 WPF 中:

public static Image GetImage(Visual target)
{
    if (target == null)
    {
        return null; // No visual - no image.
    }
    var bounds = VisualTreeHelper.GetDescendantBounds(target);

    var bitmapHeight = 0;
    var bitmapWidth = 0;

    if (bounds != Rect.Empty)
    {
        bitmapHeight = (int)(Math.Floor(bounds.Height) + 1);
        bitmapWidth = (int)(Math.Floor(bounds.Width) + 1);
    }

    const double dpi = 96.0;

    var renderBitmap =
        new RenderTargetBitmap(bitmapWidth, bitmapHeight, dpi, dpi, PixelFormats.Pbgra32);

    var visual = new DrawingVisual();
    using (var context = visual.RenderOpen())
    {
        var brush = new VisualBrush(target);
        context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
    }

    renderBitmap.Render(visual);

    return new Image
    {
        Source = renderBitmap,
        Width = bitmapWidth,
        Height = bitmapHeight
    };
}

In WPF:

public static Image GetImage(Visual target)
{
    if (target == null)
    {
        return null; // No visual - no image.
    }
    var bounds = VisualTreeHelper.GetDescendantBounds(target);

    var bitmapHeight = 0;
    var bitmapWidth = 0;

    if (bounds != Rect.Empty)
    {
        bitmapHeight = (int)(Math.Floor(bounds.Height) + 1);
        bitmapWidth = (int)(Math.Floor(bounds.Width) + 1);
    }

    const double dpi = 96.0;

    var renderBitmap =
        new RenderTargetBitmap(bitmapWidth, bitmapHeight, dpi, dpi, PixelFormats.Pbgra32);

    var visual = new DrawingVisual();
    using (var context = visual.RenderOpen())
    {
        var brush = new VisualBrush(target);
        context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
    }

    renderBitmap.Render(visual);

    return new Image
    {
        Source = renderBitmap,
        Width = bitmapWidth,
        Height = bitmapHeight
    };
}
思念满溢 2024-11-04 00:03:02

使用 WriteableBitmap 及其 渲染 功能。

在 WPF 中使用此技巧,方法是使用 RenderTargetBitmap 及其 渲染 函数

Use the WriteableBitmap and its Render function in Silverlight.

In WPF use this trick by using the RenderTargetBitmap and its Render function

虐人心 2024-11-04 00:03:02

您可以将要捕获的控件(数据绑定后的数据)添加到 ViewBox - http:// www.wpftutorial.net/ViewBox.html

从那里,您可以使用 WriteableBitmap 创建图像 - http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28VS.95%29 .aspx

You can add the controls (data after they are databound) that you want to capture to a ViewBox - http://www.wpftutorial.net/ViewBox.html

From there, you can create an image using WriteableBitmap - http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28VS.95%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文