如何在位图上捕获WPF应用程序主窗口的内容 [英] How to capture the content of the main window of a WPF application on a bitmap

查看:55
本文介绍了如何在位图上捕获WPF应用程序主窗口的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了此页面中的代码:



Mitesh Sureja的博客:如何捕获WPF应用程序的屏幕截图? [ ^ ]



因为它似乎在做我想要的是。我用一个键取下了按钮点击处理程序,然后用'Grid'替换了scrollViewer(我的程序没有),我给它命名为'gridx',如你在XAML中看到的那样。我还添加了命名空间:''使用System.IO;''



但是在行中

''UIElement element = gridx.Content as UIElement;''

'内容'这个词用红色加下划线,它说:



'网格'不包含'content'的定义,没有可访问的扩展方法'Content'可以找到接受'Grid'类型的第一个参数。 (你错过了使用指令或集会参考吗?)



你能告诉我我做错了什么吗?



谢谢



我的尝试:



I modified the code from this page:

Mitesh Sureja's Blog: How to capture screenshot of WPF application?[^]

Because it seems to do what I want. I replaced the button click handler with a key down, and I replaced the scrollViewer (which my program doesn't have ) with 'Grid' and I gave it the name 'gridx' as you see in the XAML. I also added on top the namespace: ''using System.IO;''

However in the line
''UIElement element = gridx.Content as UIElement;''
the word 'Content' is underlined red and it say:

'Grid' does not contain a definition for 'content' and no accessible extension method 'Content' accepting a first argument of type 'Grid' could be found. (are you missing a using directive or an assembly refereance?)

Can you guys tell me what i do wrong?

Thanks

What I have tried:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (linevertical.Visibility == Visibility.Hidden)
            { linevertical.Visibility = Visibility.Visible; }
            else { linevertical.Visibility = Visibility.Hidden; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (lineo.Visibility == Visibility.Hidden)
            { lineo.Visibility = Visibility.Visible; }
            else { lineo.Visibility = Visibility.Hidden; }
        }

        private void Mainwindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A)
            {
                lineo.Visibility = Visibility.Hidden;
                linevertical.Visibility = Visibility.Hidden;
            }
            if (e.Key == Key.S)
            {
                lineo.Visibility = Visibility.Visible;
                linevertical.Visibility = Visibility.Visible;

            }

            if (e.Key == Key.P)
            {
                //Set scrollviewer's Content property as UI element to capture full 
                content
                UIElement element = gridx.Content as UIElement;
                Uri path = new Uri(@"d:\temp\screenshot.png");
                CaptureScreen(element, path);
            }

        }
            public void CaptureScreen(UIElement source, Uri destination)
            {
                try
                {
                    double Height, renderHeight, Width, renderWidth;

                    Height = renderHeight = source.RenderSize.Height;
                    Width = renderWidth = source.RenderSize.Width;

                    //Specification for target bitmap like width/height pixel etc.
                    RenderTargetBitmap renderTarget = new 
                    RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, 
                    PixelFormats.Pbgra32);
                    //creates Visual Brush of UIElement
                    VisualBrush visualBrush = new VisualBrush(source);

                    DrawingVisual drawingVisual = new DrawingVisual();
                    using (DrawingContext drawingContext = 
                    drawingVisual.RenderOpen())
                    {
                        //draws image of element
                        drawingContext.DrawRectangle(visualBrush, null, new 
                        Rect(new Point(0, 0), new Point(Width, Height)));
                    }
                    //renders image
                    renderTarget.Render(drawingVisual);

                    //PNG encoder for creating PNG file
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                    using (FileStream stream = new 
                    FileStream(destination.LocalPath, FileMode.Create, 
                    FileAccess.Write))
                    {
                        encoder.Save(stream);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }        
    }
}





XAML





XAML

<Grid Name="gridx" Margin="0,0,0,13">

推荐答案

网格控件没有Content属性: ScrollViewer类(System.Windows.Controls)| Microsoft Docs [ ^ ] - 这是ScrollView控件的一部分,并从ContentControl继承。 Grid不直接或间接从ContentControl继承,因此它不会也不会具有Content属性。
The Grid control does not have a Content property: ScrollViewer Class (System.Windows.Controls) | Microsoft Docs[^] - that is part of the ScrollView control, and is inherited from ContentControl. Grid does not inherit directly or indirectly from ContentControl so it does not and will not have a Content property.


这篇关于如何在位图上捕获WPF应用程序主窗口的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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