列印預覽XAML代碼: <controls:WindowEx x:Class="SunCreate.Vipf.Client.UI.MapPrintPreview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xm ...
列印預覽XAML代碼:
<controls:WindowEx x:Class="SunCreate.Vipf.Client.UI.MapPrintPreview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:controls="clr-namespace:SunCreate.Common.Controls;assembly=SunCreate.Common.Controls" mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="1000" Loaded="WindowEx_Loaded"> <Window.Resources> <ResourceDictionary> <!-- 文字按鈕 --> <Style x:Key="stlTxtBtn" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Background="Transparent"> <Border x:Name="bd" Background="{TemplateBinding Background}" CornerRadius="2" Padding="{TemplateBinding Padding}"> <TextBlock x:Name="txt" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5 2 5 2" Foreground="White" ><InlineUIContainer> <ContentPresenter /> </InlineUIContainer></TextBlock> </Border> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="bd" Property="Background" Value="#33c4f5"/> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="txt" Property="FontSize" Value="11"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="38"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Border Background="#f0f0f0"> <Button x:Name="btnPrint" Margin="10 0 0 0" Background="#1aa4f5" Width="60" Height="28" HorizontalAlignment="Left" FontSize="12" Style="{StaticResource stlTxtBtn}" Click="btnPrint_Click" Content="列印"></Button> </Border> <Border Grid.Row="1" Background="#a0a0a0"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <!-- 該Border設置成固定A4紙大小 --> <Border Background="#fff" Margin="10" Width="842" Height="595"> <!-- 該Border可縮放至列印紙 --> <Border x:Name="border"> <Image x:Name="img" Margin="10" Stretch="Uniform" Source="{Binding Image}" HorizontalAlignment="Left" VerticalAlignment="Top" ></Image> </Border> </Border> </ScrollViewer> </Border> </Grid> </controls:WindowEx>View Code
列印預覽後臺代碼(註意:列印的是Border不是Image):
using SunCreate.Common.Controls; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Printing; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace SunCreate.Vipf.Client.UI { /// <summary> /// 地圖列印預覽 /// </summary> public partial class MapPrintPreview : WindowEx { /// <summary> /// 需要列印預覽的控制項 /// </summary> private FrameworkElement _element; private BitmapImage _Image; public BitmapImage Image { get { return _Image; } set { _Image = value; OnPropertyChanged("Image"); } } public MapPrintPreview(FrameworkElement element) { InitializeComponent(); this.DataContext = this; _element = element; } private void WindowEx_Loaded(object sender, RoutedEventArgs e) { int width = (int)_element.ActualWidth; int height = (int)_element.ActualHeight; RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); renderTargetBitmap.Render(_element); BmpBitmapEncoder bmpBitmapEncoder = new BmpBitmapEncoder(); bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); MemoryStream memoryStream = new MemoryStream(); bmpBitmapEncoder.Save(memoryStream); memoryStream.Position = 0; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); Image = bitmapImage; } private void btnPrint_Click(object sender, RoutedEventArgs e) { PrintDialog printDialog = new PrintDialog(); printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape; if (printDialog.ShowDialog() == true) { printDialog.PrintVisual(border, "列印地圖"); } } } }View Code
列印預覽效果圖(說明:地圖沒數據所以都是空白;地圖下麵空白,是因為地圖尺寸長寬比例和A4紙比例不同,縮放到A4紙):
點擊“列印”彈出系統的列印對話框。這個功能做的不是很理想,只是簡單實現了預覽與列印,沒有達到我想要的效果。