我正在尝试将silverlight控件保存到图像中,但是当控件内部有scrollviewer时会遇到麻烦。
所以我得到了我在屏幕上看到的确切图片(裁剪并使用滚动条)。 如何获得正确的结果,如下图所示?
这是xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="save" Click="ButtonBase_OnClick" Width="100" Margin="5"/>
</StackPanel>
<ScrollViewer Grid.Row="1">
<Border Background="Gray" Width="100" Height="100" BorderBrush="Black" BorderThickness="5"/>
</ScrollViewer>
</Grid>
按钮处理程序:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
ScaleTransform scaleTrans = new ScaleTransform();
double scale = 1;
scaleTrans.CenterX = 0;
scaleTrans.CenterY = 0;
scaleTrans.ScaleX = scale;
scaleTrans.ScaleY = scale;
var bitmap = new WriteableBitmap(LayoutRoot, scaleTrans);
var i = Telerik.Windows.Media.Imaging.BitmapFrame.Create(bitmap);
var dialog = new SaveFileDialog
{
Filter = "Png (*.png)|*.png",
DefaultExt = "png",
};
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
var encoder = new Telerik.Windows.Media.Imaging.PngBitmapEncoder();
encoder.Frames.Add(i);
encoder.Save(stream);
}
}
}