快照产生的低质量图片。 WPF [英] Low quality picture produced by snapshot. Wpf

查看:67
本文介绍了快照产生的低质量图片。 WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序会创建一个快照,其中包含应用程序本身主窗口的内容。然而,生成的图片的质量并不等同于Windows 10的打印屏幕程序,这会产生所需的结果。



这是程序运行的快照,使用Windows 10的打印屏幕程序,放大:



https:// ibb .co / wz4pb4d



这是以下程序制作的快照,放大:



https://ibb.co/DLsNb8X



有什么我们可以尝试提高该程序产生的快照的质量?



我尝试了Bitmap Encoder,但结果相同,只是没有透明度,(不需要透明度)也试过其他Pixel格式但是我收到了错误,只有Pbgra32似乎可以用作该程序。



提前谢谢



我是什么尝试过:



The program below creates a snapshot with the content of the main window of the application itself. However the quality of the produced picture is not equivalent to the print screen program of windows 10, which produces the desired result.

Here is a snapshot of the program running, taken with the print screen program of windows 10, zoomed in:

https://ibb.co/wz4pb4d

And here is the snapshot that the program below is producing, zoomed in:

https://ibb.co/DLsNb8X

Is there something we can try to improve the quality of the snapshot that this program produse?

I tried Bitmap Encoder but is the same result , just without transparency, (Don't need to have transparency) also tried some other Pixel Formats but I get errors, only Pbgra32 seems to work as the program is.

Thanks in advance

What I have tried:

<pre> if (e.Key == Key.P)
            {
                //Set scrollviewer's Content property as UI element to capture full content
                FrameworkElement element = UxVisual as FrameworkElement;
                Uri path = new Uri(@"C:\Users\4gry\Desktop\screenshot.png");
                CaptureScreen(element, path);
            }

        }
            public void CaptureScreen(FrameworkElement source, Uri destination)
            {
            RenderOptions.SetEdgeMode(source, EdgeMode.Aliased);
            try
                {
                    double Height, ActualHeight, Width, ActualWidth;

                    Height = ActualHeight = source.RenderSize.Height;
                    Width = ActualWidth = source.RenderSize.Width;

                    //Specification for target bitmap like width/height pixel etc.
                    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)ActualWidth, (int)ActualHeight, 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(source.RenderSize));
                    }
                    

                    //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

<Window x:Name="mainwindow" x:Class="WpfApp2.MainWindow"
        
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="mainwindow" Height="397.265" Width="603.147" Icon="images2/Untitled-1.gif" ResizeMode="CanMinimize" WindowStartupLocation="Manual" AutomationProperties.Name="Grid" IsTabStop="True" KeyDown="Mainwindow_KeyDown" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0">
    <Border x:Name="UxVisual" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="367" Margin="10,0,0,0" VerticalAlignment="Top" Width="583">
        <Grid Margin="0,0,0,13">

            <Image Stretch="None" Source="images2/screenshot copy.gif" IsEnabled="False" Margin="0,0,-28,0" HorizontalAlignment="Left" Width="625" Height="347" VerticalAlignment="Top"/>

            <Image x:Name="linevertical" HorizontalAlignment="Left" Height="343" Margin="108,76,-21,0" VerticalAlignment="Top" Width="705" Source="images2/Untitled-1.gif" Stretch="None" Visibility="Hidden" RenderTransformOrigin="0.46,0.52"/>
            <Image x:Name="lineo" HorizontalAlignment="Left" Height="171" Margin="173,76,0,0" VerticalAlignment="Top" Width="193" Source="images2/Untitled-3.gif" Stretch="None" Visibility="visible"/>
            <Image HorizontalAlignment="Left" Height="100" Margin="10,16,0,0" VerticalAlignment="Top" Width="189" Source="images2/Untitled-1.gif" Stretch="None" Visibility="hidden"/>
            <Image HorizontalAlignment="Left" Height="100" Margin="73,10,0,0" VerticalAlignment="Top" Width="100" Source="images2/Untitled-3.gif" Stretch="None" Visibility="visible"/>
            <Button HorizontalAlignment="Left" Margin="173,61,0,0" VerticalAlignment="Top" Width="225" Height="121" Opacity="0"  Click="Button_Click"/>
            <Button Content="" HorizontalAlignment="Left" Margin="464,247,0,0" VerticalAlignment="Top" Width="194" Height="144" Opacity="0" Click="Button_Click_1"/>
            <Image HorizontalAlignment="Left" Height="100" Margin="428,-11,-40,0" VerticalAlignment="Top" Width="206" Stretch="None" Source="images2/Untitled-1.gif"/>

        </Grid>
    </Border>
</Window>

推荐答案

我创建了我想捕获的Visual(即FrameworkElement)然后使用ACTUAL尺寸:

I create the "Visual" (i.e. FrameworkElement) that I want to capture and then use the ACTUAL dimensions:
RenderTargetBitmap rtb = new RenderTargetBitmap(
            (int) printVisual.ActualWidth,
            (int) printVisual.ActualHeight, 96, 96, PixelFormats.Pbgra32 );







Quote:

FrameworkElement是基于UIElement构建的WPF框架级实现类,并添加了与WPF框架级别的特定交互。 FrameworkElement添加并定义了以下功能:



其他特定于框架的布局特征



支持更丰富的元数据报告属性



某些输入基类及其附加属性或附加事件的特定于类的实现



风格支持



进一步动画支持

FrameworkElement is the WPF framework-level implementation class that builds on UIElement, and adds specific interactions with the WPF framework level. FrameworkElement adds and defines the following capabilities:

Additional framework-specific layout characteristics

Support for richer metadata reporting on properties

Class-specific implementation of certain input base classes and their attached properties or attached events

Style support

Further animation support





换句话说,您应该使用FrameworkElement而不是UI元素。我经常在主目标视觉周围使用对边框(可见或不可见)的引用。



In other words, you should be working with FrameworkElement and not UI element. I frequently use a reference to a "Border" (visible or not) around my main target visual.


PixelFormats.Pbgra32



尝试使用更高分辨率的格式。


Try a format with a higher resolution.


这篇关于快照产生的低质量图片。 WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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