winForm 打印预览

摘要:
我很少写科技博客。虽然我已经做了两年多的程序员,但我从事过winform开发和web开发。我知道。net相关技术,如mvc、wcf、wpf、silverlight、socekt communication、nhibernate、spring,无论Netwp手机开发项目有多大,但最终发现软件架构比所谓的单一功能更系统。

  自己很少写技术博客,虽然已经干程序员两年多了,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 打印预览》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇动态将ADOQuery数据移植到ClientDataSet通用函数添加串口和虚拟终端输出帮助调试下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#数组段ArraySegment<T>的使用

//数组段ArraySegment<T>的使用 using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; namespace ss { class Program...

Python学习之模块进程函数详解

  今天在看《Beginning Linux Programming》中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Python的 os 模块实现了对应的函数封装了这些系统调用: os.system , os.exec , os.fork , os.wait,本文和大家分享的就是这部分内...

awk 调用 shell 命令,并传递参数

from:awk 调用 shell 命令的两种方法:system 与 print shell 向awk传递命令,这样使用即可: awk -v  ...  但反过来呢?awk调用外部命令,同时也传参呢?  awk 中使用的 shell 命令,有 2 种方法:一。使用所以 system()awk 程序中我们可以使用 system() 函数去调用 shell...

c# winform 智能模糊匹配 输入框

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Windows.Forms;usi...

WinForm实现简单的拖拽功能(C#)

用到了ListBox和TreeView两个控件,ListBox作为数据源,通过拖拽其中的数据放置到TreeView上,自动添加一个树节点 ListBox控件的MouseDown用于获取要拖拽的值并调用DoDragDrop方法 privatevoidlistBox1_MouseDown(objectsender,MouseEventArgse){//调用Do...

T4教程1 T4模版引擎之基础入门

T4模版引擎之基础入门    额,T4好陌生的名字,和NuGet一样很悲催,不为世人所熟知,却又在背后默默无闻的奉献着,直到现在我们项目组的人除了我之外,其它人还是对其豪无兴趣,基本上是连看一眼都懒得看,可怜的娃啊。。。   T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中...