自己很少写技术博客,虽然已经干程序员两年多了,winform开发,web开发都干过,不论项目大小对于.net的相关技术也是了解的,如mvc,wcf,wpf,silverlight,socekt通讯,nhibernate,spring.net wp手机开发等,但是最终发现还是软件架构比所谓的单一功能更为系统化。

下面是一个小的例子 Winfom打印预览

首先是基类DocumentBase继承系统的PrintDocument

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms; namespace prientDemo
{
public class DocumentBase:PrintDocument
{
public DialogResult ShowPrintPreviewDialog()
{
PrintPreviewDialog dialog = new PrintPreviewDialog();
dialog.Document = this;
return dialog.ShowDialog(); }
}
}

然后ImageDocument再继承ImageDocument

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace prientDemo
{
public class ImageDocument:ImageDocument
{
private Image _image;
public Image Image
{ get { return _image; } set { _image = value; } }
public ImageDocument()
{ }
public ImageDocument(Image image)
{ this.Image = image; }
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{
if(Image==null)
{
throw new InvalidOperationException();
}
e.Graphics.DrawImage(Image, e.MarginBounds);
}
}
}

以上是做打印功能时需要自己写的两个类,

下面是winform窗体的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace prientDemo
{
public partial class LoadPicture : Form
{
public LoadPicture()
{
InitializeComponent();
}
OpenFileDialog pfd = new OpenFileDialog();
private DocumentBase _document;
private void Load_Click(object sender, EventArgs e)
{
if(pfd.ShowDialog(this)==DialogResult.OK){
try
{
pictureImage.Image = Image.FromFile(pfd.FileName);
_document = new ImageDocument(pictureImage.Image);
}
catch (Exception ex)
{
MessageBox.Show("This image could not be loaded."+ex.Message);
} } } private void Preview_Click(object sender, EventArgs e)
{
if(_document==null)
{
MessageBox.Show("You must load an image first");
return;
}
_document.ShowPrintPreviewDialog();
}
}
}

下面就完成了,运行效果:如下图

winForm 打印预览的更多相关文章

  1. Winform中实现ZedGraph曲线图的图像复制到剪切板、打印预览、获取图片并保存、另存为的功能

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  2. C#教程之打印和打印预览

    最近研究一了一下关于PDF打印和打印预览的功能,在此小小的总结记录一下学习过程. 实现打印和打印预览的方法,一般要实现如下的菜单项:打印.打印预览.页面设置. PrintDocument类 Print ...

  3. .NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现

    原文:.NET环境下有关打印页面设置.打印机设置.打印预览对话框的实现 我个人认为,开发MIS,首先就得解决网格的问题,而开发工具为我们提供了如DataGrid.MSHFlexGrid的控件.其次,是 ...

  4. 怎么在MindManager中查看打印预览

    在MindManager2016思维导图中打印导图之前,可以先进行预览,MindManager和其他很多应用程序一样都带有打印预览功能,该功能提供了再次检查的机会,避免打印出错,MindManager ...

  5. 关闭rdlc报表打印预览后,关闭客户端,抛出异常“发生了应用程序级的异常 将退出”

    问题:关闭rdlc报表打印预览后,关闭客户端,抛出异常“发生了应用程序级的异常 将退出” 办法:在容纳ReportViewer的窗体后台代码中,添加如下代码即可 protected override ...

  6. .NET网页打印以及使用打印需要注意的事项(可能会引起VS崩溃的现象、打印预览后关闭功能不管用)

    这两天进行给网页添加打印.打印预览.页面设置的功能.遇到了以下几个问题 [1]在网上查找了一些打印方法,一开始还可以用,后来不知道动到了哪里,点击vs中拆分或者切换到另一个设计和源代码显示方式,就会引 ...

  7. QNDataSet打印预览自动关闭问题

    问题:打印预览后,数据集自动关闭 解决: TQNDataSet = class(TFDMemTable) private protected procedure PSReset; override; ...

  8. 基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作

    在前面介绍了很多篇相关的<Bootstrap开发框架>的系列文章,这些内容基本上覆盖到了我这个Bootstrap框架的各个主要方面的内容,总体来说基本达到了一个稳定的状态,随着时间的推移可 ...

  9. JS 打印功能代码可实现打印预览、打印设置等

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

随机推荐

  1. Python学习 之 走进python

    1.Python:是一种解释型的.面向对象的.带有动态语义的高级程序设计语言. 2.Python发展阶段 —CNRI时期 —BeOpen时期 —DC时期 —Python 3.0 Python里程碑:2 ...

  2. Cache和Buffer的区别

    一.研究数据库的人这样理解:http://wenku.baidu.com/view/32b8b13e376baf1ffc4fad7e.html Cache和Buffer是两个不同的概念,简单的说,Ca ...

  3. Redis 服务器

    Redis 服务器命令主要是用于管理 redis 服务. 实例 以下实例演示了如何获取 redis 服务器的统计信息: redis 127.0.0.1:6379> INFO # Server r ...

  4. ethtool 在 Linux 中的实现框架和应用

    转载:http://www.ibm.com/developerworks/cn/linux/1304_wangjy_ethtools/index.html?ca=dat- 王 俊元, 软件工程师, I ...

  5. 各种电子面单_Api接口

    电子面单是一种通过热敏纸打印输出纸质物流面单的物流服务.通过热感应显示文字,打印速度比传统针式打印速度提升4~6倍.电子面单以接口形式嵌入到自己的系统.网站上,可以在自己的平台操作打印电子面单.   ...

  6. J - 病毒

    J - 病毒 Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status ...

  7. javacv

    (看到有很多同学都来看这篇文章,说明可能是有必要的,然后这个写的比较水,所以 如果求干货的话,请移步: http://www.cnblogs.com/letben/p/5885799.html 但是 ...

  8. LeetCode 80

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates": What if duplicat ...

  9. [改善Java代码]若有必要,使用变长数组

    Java中的数组是定长的,一旦经过初始化声明就不可改变长度,这在实际使用的时候非常不方便.比如要对一个班级的学生信息进行统计,因为我们不知道班级会有多少个学生(随时可能有退学,入学,转学),所以需要一 ...

  10. 转 oracle 11g 导出空表

    1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出. 2.设置deferred_segment_creation 参数为FALSE后,无论是 ...