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

打印预览后台代码(注意:打印的是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, , , PixelFormats.Pbgra32);
renderTargetBitmap.Render(_element);
BmpBitmapEncoder bmpBitmapEncoder = new BmpBitmapEncoder();
bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
MemoryStream memoryStream = new MemoryStream();
bmpBitmapEncoder.Save(memoryStream);
memoryStream.Position = ;
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, "打印地图");
}
} }
}

打印预览效果图(说明:地图没数据所以都是空白;地图下面空白,是因为地图尺寸长宽比例和A4纸比例不同,缩放到A4纸):

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

WPF实现可视化控件打印及打印预览的更多相关文章

  1. 用Synoptic Panel自定义基于图形的可视化控件--制作一张剧场售票统计报表

    数据可视化的一大特点就是能给报表使用者带来感官上的享受.不再是枯燥的数字,而变成一个一个亮丽的图形.之前业界大神公布过一个统计Car Accidents的报表,这个Power BI Report的特点 ...

  2. Lodop中特殊符号¥打印设计和预览不同

    Lodop中¥符号样式改变问题 Lodop中对超文本样式的解析,虽然说是按照调用的本机ie引擎,但是调用的ie版本可能不同,导致在ie下是一种样式,预览又是另一种样式.可能是有些样式没有具体设置,走的 ...

  3. asp.net调用Lodop实现页面打印或局部打印,可进行打印设置或预览

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebPrint.aspx.cs ...

  4. JS Web打印,实现预览新样式

    问题描述:     JS实现Web打印,要求打印前一种样式,打印预览时新样式 问题解决:         (1)设置打印时的css样式,设置打印前的css样式 注:         以上为print. ...

  5. 自定义type为file型input控件+该控件具有本地图片预览功能

    最近的一个项目需求是写一个type为filex型的input控件,这个控件: 第一,要自定义样式: 第二,要能直接在本地预览上传的图片: 第三,要能检测图片的尺寸是否符合要求. 故综合网上的资源写了下 ...

  6. WPF可视化控件打印

    Introduction While coding an application that displays a detailed report in a ScrollViewer, it was d ...

  7. css去掉使用bootstrap框架后打印网页时预览效果下的超链接

    在我们写网页的时候,超链接是链接各个页面的桥梁,也是搜索引擎爬虫(spider)收录网站页面的关键,因此,在每个网页中会有许多的超链. 今天,一个同行妹妹在使用了bootstrap框架来搭建自己的网站 ...

  8. LODOP打印控件进行批量打印

    Lodop打印控件批量打印的方式:1.批量打印每页内容相同的:(1)批量打印相同内容的很多纸张,可以设置打印份数,把该内容打印出多份.2.批量打印每页不同内容的:(1)通过在一个任务中分页,循环添加页 ...

  9. C#、winform、wpf将类控件放进工具箱里

    有时我们需要将vs自带的控件的某一些方法或属性进行一些修改,我们通常会新建一个类来继承它然后对它的方法或属性进行修改,那么我们如何将修改完成的控件类变成可视化控件放到工具箱中便于使用呢? 很简单,只要 ...

随机推荐

  1. vue 设置button disabled

    <button v-bind:disabled="dis" @click="alert">button</button> dis:'' ...

  2. 使用JavaScript实现表现和数据分离

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  3. mysql bigint ,int , smallint,tinyint 的范围

    bigint  8字节 64位 int 4字节 32位 smallint 2字节 16位 tinyint 1字节8位 .. 范围  -128 到 127  , 如果是无符号 ,则返回 位 0-255 ...

  4. 1.about

    1)about Evarobot a.Evarobot Tech Specs 2)应用场景 Using a PC running visualisation/monitoring software a ...

  5. 解决mysql安装出现error Nr.1045问题

    我们在windows下安装mysql最后一步时会出现Access denied for user 'root'@localhost'(using password:No)的问题.这几个问题经常出现在卸 ...

  6. 人类及其他物种基因组DNA之问

    问题1 : 不同人类个体的基因组长度总长是不是一样,如果不一样,那么人类基因组长度排序和范围区间是如何控制的?最短是多少,最长是多少?如果一样,如何理解基因的插入与缺失,INDEL等现象,如何平衡的呢 ...

  7. 使用adb shell 模拟点击事件

    针对问题[appium无法点击到某一内容,需要使用adb去执行点击事件] 需要命令: adb shell adb devices input [input可以实现的功能]: 输入文本信息:input ...

  8. java:static详解

    1.static修饰的变量习惯称为静态变量,static修饰的方法称为静态方法,static修饰的代码块叫做静态代码块. 1)static变量 static变量也称作静态变量,静态变量和非静态变量的区 ...

  9. Java的GUI设计中如何跨界面传值

    在Java设计中我们会遇到登录界面的信息,在后面的某个情况也需要使用. 比如这是笔者的一个登录界面 可以看到获取密码和账号 在这个时候的功能的完成需要密码和账号 // 登录信息的获取 public S ...

  10. linux之网络

    一 什么是网络,网络能干什么 网络出现的主要目的就是实现主机和主机之间的通信,而互联网协议(Internet Protocol Suite)就是连接两台计算机之间的internet一系列统一的标准.互 ...