【发布时间】:2020-04-04 18:21:22
【问题描述】:
我在question 的第一个答案中实现了代码,它工作得很好,只是它不包含标题栏,这是我的必需品。有人知道我做错了什么吗?
我更新了我的示例以显示所有其他已注释掉的失败尝试。 这是我的代码:
Window shellView = Application.Current.MainWindow;
Rect bounds = VisualTreeHelper.GetDescendantBounds(shellView);
//Rect bounds = new Rect(new Size(shellView.ActualWidth, shellView.ActualHeight));
//Rect bounds = shellView.RestoreBounds;
//Rect bounds = VisualTreeHelper.GetContentBounds(shellView);
//Rect bounds = new Rect(new Size(VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualWidth,
// VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualHeight));
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
string fileName = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\Screen_Capture.png";
if (_dialogService.ShowSaveFileDialog(ref fileName, "PNG Files | *.png"))
{
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(shellView);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}
renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(fileName))
{
bitmapEncoder.Save(stm);
}
}
应 XAMIMAX 的要求,我可以分享更改名称的 XAML:
<Window x:Class="MyProject.Shell.Views.Shell.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ap="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
xmlns:controls="clr-namespace:MyProject.Common.Support.Controls;assembly=MyProject.Common.Support"
xmlns:converters="clr-namespace:MyProject.Common.Support.Converters;assembly=MyProject.Common.Support"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dts="clr-namespace:MyProject.Common.Support.DataTemplateSelectors;assembly=MyProject.Common.Support"
xmlns:enums="clr-namespace:MyProject.Shell.Enums"
xmlns:enums1="clr-namespace:MyProject.Common.Support.Enums;assembly=MyProject.Common.Support"
xmlns:ikriv="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:m="clr-namespace:MyProject.Common.Support.MarkupExtensions;assembly=MyProject.Common.Support"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resources="clr-namespace:MyProject.Shell.Views.Shell.Resources"
xmlns:ss="clr-namespace:MyProject.Common.Support.StyleSelectors;assembly=MyProject.Common.Support"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:views="clr-namespace:MyProject.NotificationModule.Client.Views;assembly=MyProject.NotificationModule.Client"
xmlns:constants="clr-namespace:MyProject.Shell"
x:Name="shell"
Title="{Binding MyProjectApplicationTitle}"
Width="1024"
Height="768"
MinWidth="800"
MinHeight="600"
Background="{DynamicResource PrimarySolidColorBrush}"
Icon="/Resources/Embedded/MyProject.ico"
mc:Ignorable="d">
<controls:LocalizationScope.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.Children>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NavigationBarWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MinWidth="38"
MaxWidth="400" />
</Grid>
</Grid.Children>
</Grid>
</controls:LocalizationScope.Content>
</Window>
我希望 XAML 足以提供帮助。我还尝试了此链接Generating a screenshot of a WPF window 上的示例,但得到了相同的结果:没有标题栏。
【问题讨论】:
-
我这边也显示了标题栏,我可以看看你的Window的代码来了解我的和你的有什么不同吗?
-
谢谢,@nalka,但它很大,而且里面有很多专有的东西,我不想张贴。我能说的是,应用程序主窗口,后面的ShellView类的代码是这样声明的:_public partial class ShellView : Window, IShell, IVisibleShell _ 所以肯定是继承自Window类的。
-
您是否将窗口修改为自定义标题栏? (这是通过使用
WindowStyle="None"和AllowsTransparency="True"实现的) -
不。使用标准标题栏:Title="{Binding MyApplicationTitle}"
-
您是否尝试过使用 window 中的 actualWidth 和 ActualHeight ?而 shellView 只会引起关注。这可能是 winforms 和 wpf 的混搭,没有任何实践,而且到处都是黑客
标签: wpf screenshot