WPF实现可视化控件打印及打印预览

打印预览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纸):

点击“打印”弹出系统的打印对话框。这个功能做的不是很理想,只是简单实现了预览与打印,没有达到我想要的效果。

原文地址:https://www.cnblogs.com/s0611163/p/10147705.html