使用以下代码:
Dispatcher.BeginInvoke(
() =>
{
byte[] imageBytes = null;
WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap(_Width, _Height);
bmpCurrentScreenImage.Render(DrawingSurfaceBackground, new MatrixTransform());
bmpCurrentScreenImage.Invalidate();
using (var stream = new MemoryStream())
{
bmpCurrentScreenImage.SaveJpeg(stream, bmpCurrentScreenImage.PixelWidth,
bmpCurrentScreenImage.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
imageBytes = stream.ToArray();
}
});
其中DrawingSurfaceBackground是MainPage.xaml的属性。
<phone:PhoneApplicationPage
x:Class="Roulettist.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}" Orientation="Landscape" SupportedOrientations="Landscape" BackKeyPress="PhoneApplicationPage_BackKeyPress" OrientationChanged="PhoneApplicationPage_OrientationChanged">
<!--LayoutRoot is the root grid where all page content is placed-->
<DrawingSurfaceBackgroundGrid x:Name="DrawingSurfaceBackground" Loaded="DrawingSurfaceBackground_Loaded">
</DrawingSurfaceBackgroundGrid>
</phone:PhoneApplicationPage>
我正在尝试获取该应用的截图,但我收到了黑色像素的纹理。 我看到有些人遇到同样的问题,但在任何地方都没有看到问题的答案。
那么有没有办法在XAML + Direct3D应用程序中使用UIElement.Render方法获取屏幕截图?