自UIElement生成的BitmapSource [英] Generate BitmapSource from UIElement

查看:260
本文介绍了自UIElement生成的BitmapSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成一个 BitmapFrame 这是基于的UIElement 。这里是我的功能:

I am attempting to generate a BitmapFrame that is based on a UIElement. Here is my function:

private BitmapFrame RenderToBitmap2()
{
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);

    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    VisualBrush aVisualBrush = new VisualBrush(GenerateTestStackPanel());
    drawingContext.DrawRectangle(aVisualBrush, new Pen(Brushes.Green, 2), new Rect(new Size(150, 150)));

    drawingContext.Close();

    renderBitmap.Render(drawingVisual);

    return BitmapFrame.Create(renderBitmap);
}

有关测试和调试的目的,我使用的创建一个简单的StackFrame说的附加功能应的创建可重复presented有效的视觉元素:

For testing and debugging purposes, I am using an additional function that creates a simple StackFrame that should create a valid visual element that can be represented:

private StackPanel GenerateTestStackPanel()
{
    // Create a red Ellipse.
    Ellipse myEllipse = new Ellipse();

    myEllipse.Fill = Brushes.Green;
    myEllipse.StrokeThickness = 2;
    myEllipse.Stroke = Brushes.Black;

    // Set the width and height of the Ellipse.
    myEllipse.Width = 200;
    myEllipse.Height = 200;
    // Add the Ellipse to the StackPanel.
    StackPanel myStackPanel = new StackPanel();
    myStackPanel.Children.Add(myEllipse);
    return myStackPanel;
}

有关某种原因,的VisualBrush不被呈现在DrawRetangle(...)的功能。我可以看到绿色的边框,但没有别的。另外,如果我换出的VisualBrush与标准刷,它的伟大工程:

For some reason, the VisualBrush is not being rendered in the DrawRetangle(...) function. I can see the green border but nothing else. In addition, if I swap out the VisualBrush with a standard brush, it works great:

drawingContext.DrawRectangle(Brushes.Plum, new Pen(Brushes.Green, 2), new Rect(new Size(150, 150)));

在此先感谢!

推荐答案

看看本作的替代方法来创建一个的BitmapSource 的UIElement

Take a look at this for an alternate way to create a BitmapSource from a UIElement:

<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb84fbcb-fe36-45a7-bc1a-0dd0d0bb9fdb/\"相对=nofollow> MSDN线程

我也一直试图获得的VisualBrush没有它给我带来了这个线程任何侥幸工作。

I have also been trying to get the VisualBrush to work without any luck which brought me to this thread.

public static BitmapSource CreateBitmapSourceFromVisual(
    Double width,
    Double height,
    Visual visualToRender,
    Boolean undoTransformation)
    {
        if (visualToRender == null)
        {
            return null;
        }
        RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width),
            (Int32)Math.Ceiling(height), 96, 96, PixelFormats.Pbgra32);

        if (undoTransformation)
        {
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(visualToRender);
                dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
            }
            bmp.Render(dv);
        }
        else
        {
            bmp.Render(visualToRender);
        }
      return bmp;
    }

这篇关于自UIElement生成的BitmapSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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