自己很少写技术博客,虽然已经干程序员两年多了,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. Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 字符串

    1.辅助方法 2.字符串 输入“irb”进入Ruby命令行开发环境,控制器的启动方法是在命令行中执行“rails console”. (1)字符串连接 >>"foo"+ ...

  2. 实验-hadoop新增节点

    关于hadoop新增节点网上的说法都有些差别,自己来实践一把 1.建立一个namenode一个datanode的集群 master:192.168.126.130 slave1:192.168.126 ...

  3. 聊聊 #pragma 和 // MARK:

    我去,就这两个东西还要讲?是OC或Swift开发人员都知道是怎么回事好吗?不就是用来标记和分组代码的吗?难道还有别的装逼技能? 当然,其实问大部分人说这两个是什么作用,或者是除了这两个还知道什么的情况 ...

  4. Npoi 导出Excel 下拉列表异常: String literals in formulas can't be bigger than 255 Chars ASCII

    代码: public static void dropDownList(string[] datas, string filePath) { HSSFWorkbook workbook = new H ...

  5. Entity Framework实体框架使用TrackerEnabledDbContext进行操作日志跟踪

    在EF实体框架中进行日志跟踪,一般都是自己写个Log实体类,在数据保存时进行属性原始值验证来进行日志跟踪.当然还可以使用一些第三扩展库例如:entity framework extended进行日志记 ...

  6. G - Just a Hook

    Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  7. JDK1.7中调用javascript方法

    import java.io.File; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.s ...

  8. SQL把做个字段组合成一个字符串

  9. Debian 7 安装使用 Virtualbox及增强功能

    一.安装virtualbox 可以从源里安装 sudo apt-get install virtualbox 也可以下载最新版安装 https://www.virtualbox.org/wiki/Do ...

  10. Python Virtualenv 虚拟环境

    在python2和python3共存的机器上做开发,要想互不干扰,虚拟环境很重要. Debian7 默认是python2.7.3,装好python3.4.1后怎么建立虚拟环境呢? $ pyvenv m ...