如何“拍照"在 WPF 中运行时在后面的代码中创建的帧? [英] How to "take a photo" of a frame that was created in the code behind during run-time, in WPF?

查看:17
本文介绍了如何“拍照"在 WPF 中运行时在后面的代码中创建的帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数,它是从按钮 Click 事件处理程序调用的:

I have this function, which is called from a button Click event handler:

private void CreateFrame(Page page)
{
    Frame newFrame = new Frame();
    newFrame.Navigate(page);

    // FramesHolder is a StackPanel with multiple Frames inside it
    FramesHolder.Children.Add(newFrame);

    // The size the page is 525x50
    RenderTargetBitmap renderTargetBitmap =
        new RenderTargetBitmap(525, 50, 96, 96, PixelFormats.Pbgra32);
    renderTargetBitmap.Render(newFrame);
    PngBitmapEncoder pngImage = new PngBitmapEncoder(); 

    pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
    using (Stream fileStream = File.Create("Frame.png"))
    {
        pngImage.Save(fileStream);
    }
}

问题是 Frame.png 图像呈现黑色.如何解决?谢谢.

The problem is that the Frame.png image is rendered black. How to fix it? Thanks.

推荐答案

我将 renderTargetBitmap.Render(newFrame); 行更改为 renderTargetBitmap.Render(page);它成功地截取了屏幕截图.

I changed the renderTargetBitmap.Render(newFrame); line to renderTargetBitmap.Render(page); and it successfully takes the screenshot.

问题在于您的新框架的创建.

The problem is in the creation of your new frame.

我显示了一个页面,在 Loaded 事件中,我使用上面指定的更改调用了您的方法,并且它可以工作.

I displayed a page and in the Loaded event i called your method with the change i specified above and it works.

public partial class Page2 : System.Windows.Controls.Page
{
    public Page2()
    {
        InitializeComponent();
    }

    private void PG2_Loaded(object sender, RoutedEventArgs e)
    {
        CreateFrame(this);
    }

    private void CreateFrame(Page page)
    {
        Frame newFrame = new Frame();
        newFrame.Navigate(page);

        // The size the page is 525x50
        RenderTargetBitmap renderTargetBitmap =
            new RenderTargetBitmap(525, 50, 96, 96, PixelFormats.Pbgra32);
        renderTargetBitmap.Render(page);
        PngBitmapEncoder pngImage = new PngBitmapEncoder();

        pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
        using (Stream fileStream = File.Create("c:\\Frame.png"))
        {
            pngImage.Save(fileStream);

        }
    }

}

这篇关于如何“拍照"在 WPF 中运行时在后面的代码中创建的帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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