打印预览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. CentOS 7安装配置Samba服务器(挂载共享文件夹)

    CentOS 7安装配置Samba服务器 CentOS 7下Samba服务器安装配置过程笔记. 假设我们有这样一个场景 共享名 路径 权限 SHAREDOC /smb/docs 所有人员包括来宾均可以 ...

  2. The prefix "mvc" for element "mvc:annotation-driven" is not bound 异常

    https://www.cnblogs.com/maodot/p/7531042.html The prefix "mvc" for element "mvc:annot ...

  3. zookeeper相关

    1.zookeeper应用:集群节点间的数据同步(资源管理),分布式锁(主要是利用客户端在一个会话中在zookeeper中创建一个znode节点,然后再去执行自己的业务代码,比如去更新数据库,其他客户 ...

  4. jvm层面锁优化+一般锁的优化策略

    偏向锁: 首先了解对象头MARK指针(对象头标记,32位): 存储GC标记,对象年龄,对象Hash,锁信息(锁记录的指针,偏向锁线程的ID) 大部分情况是没有竞争的,所以可以通过偏向来提高性能 所谓的 ...

  5. Google Reader 快关了!!

    现在还每天用Google Reader, 每次打开都提示7月1号要关闭... 上图怀念: 控制区功能:排序.展开\收缩显示.上一条\下一条,还有下拉框下的很多功能... 列表显示 针对每个Item下的 ...

  6. Tarjan 割点,桥

    /* ggg ggg ggggggg ggggggg ggggggggggggggggggg ggggggggggggggg ggggggggggg ggggggg ggg g */ /* gyt L ...

  7. 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)

    传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi​表示第i ni~ ni n位第一个 ...

  8. 2018.10.27 loj#2292. 「THUSC 2016」成绩单(区间dp)

    传送门 g[i][j][k][l]g[i][j][k][l]g[i][j][k][l]表示将区间l,rl,rl,r变成最小值等于kkk,最大值等于lll时的花费的最优值. f[i][j]f[i][j] ...

  9. 假期训练七(hdu-2845 dp,hdu-1846,2188 巴什博奕)

    题目一:传送门 思路:动态规划,从每一行来看,每次更新求出这一点的最大值,dp[i]=MAX(dp[i-1],dp[i]+dp[i-2]),不会出现 两个数字相邻的情况:先对行进行更新,再对列进行更新 ...

  10. 关于对话框不能响应OnKeyDown和OnChar函数的一些说明

    (1)现象  在MFC的对话框中,映射了WM_CHAR和WM_KEYDOWN消息响应函数后,还是不能响应OnKeyDown和OnChar. (2)原因  因为MFC在进行设计的时候,这两个消息被对话框 ...