当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Export whole silverlight control to image

问题描述:

I'm trying to save silverlight control to an image, but getting troubles when control has scrollviewer inside.

So i'm getting the exact picture i see on the screen (cropped and with scrollbar).

How to get correct result which should look like on the picture below?

Here is the 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>

And button handler:

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);

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: