using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace SYS_TEST.BaseClass
{/// <summary>/// 图片预览打印/// </summary>public class PrintPreviewClass{private string[] lines = null;private Image image = null;private string StreamType = "";private Stream StreamToPrint = null;private Font mainFont = new Font("宋体", 12);private string fileName = "";private PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();private PrintDocument pdDocument = new PrintDocument();private PrintPreviewControl printPreviewControl1;public void PrintPreview(string filepath, string filetype){this.fileName = Path.GetFileNameWithoutExtension(filepath);//BeginPrint事件pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);//PrintPage事件pdDocument.PrintPage += new PrintPageEventHandler(pdDocument_PrintPage);//EndPrint事件pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);//StartPrint打印方法FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);StartPrint(fs, filetype);}/// <summary>/// StartPrint打印方法/// </summary>/// <param name="streamToPrint"></param>/// <param name="streamType"></param>public void StartPrint(Stream streamToPrint, string streamType){//声明返回值的PageSettingsPageSettings ps = new PageSettings();//显示设置打印页对话框PageSetupDialog Psdl = new PageSetupDialog();//打印多页设置PrintDialog pt = new PrintDialog();pt.AllowCurrentPage = true;pt.AllowSomePages = true;pt.AllowPrintToFile = true;//form中的打印预览printPreviewControl1.Document = pdDocument;printPreviewControl1.Zoom = 1.0;printPreviewControl1.Dock = DockStyle.Fill;printPreviewControl1.UseAntiAlias = true;this.StreamToPrint = streamToPrint;//打印的字节流this.StreamType = streamType; //打印的类型pdDocument.DocumentName = this.fileName; //打印的文件名Psdl.Document = pdDocument;this.printPreviewDialog.Document = pdDocument;this.printPreviewDialog.SetDesktopLocation(300, 800);Psdl.PageSettings = pdDocument.DefaultPageSettings;pt.Document = pdDocument;try{if (Psdl.ShowDialog() == DialogResult.OK){ps = Psdl.PageSettings;pdDocument.DefaultPageSettings = Psdl.PageSettings;}if (pt.ShowDialog() == DialogResult.OK){pdDocument.PrinterSettings.Copies = pt.PrinterSettings.Copies;}if (this.printPreviewDialog.ShowDialog() == DialogResult.OK)pdDocument.Print();}catch (InvalidPrinterException ex){MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);throw;}}/// <summary>/// BeginPrint事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void pdDocument_BeginPrint(object sender, PrintEventArgs e){char[] param = { '\n' };char[] trimParam = { '\r' };switch (this.StreamType){case "txt":StringBuilder text = new StringBuilder();StreamReader streamReader = new StreamReader(this.StreamToPrint, Encoding.Default);while (streamReader.Peek() >= 0){lines = streamReader.ReadToEnd().Split(param);for (int i = 0; i < lines.Length; i++){lines[i] = lines[i].TrimEnd(trimParam);}}break;case "image":image = System.Drawing.Image.FromStream(this.StreamToPrint);break;default:break;}}/// <summary>/// PrintPage事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void pdDocument_PrintPage(object sender, PrintPageEventArgs e){int linesPrinted = 0;int leftMargin = Convert.ToInt32((e.MarginBounds.Left) * 3 / 4);  //左边距int topMargin = Convert.ToInt32(e.MarginBounds.Top * 2 / 3);    //顶边距switch (this.StreamType){case "txt":while (linesPrinted < lines.Length){//向画布中填写内容e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, leftMargin, topMargin, new StringFormat());topMargin += 55;//行高为55,可调整//走纸换页if (topMargin >= e.PageBounds.Height - 60)//页面累加的高度大于页面高度。根据自己需要,可以适当调整{e.HasMorePages = true;printPreviewControl1.Rows += 1;//窗体的打印预览窗口页面自动加1/** PrintPageEventArgs类的HaeMorePages属性为True时,通知控件器,必须再次調用OnPrintPage()方法,打印一个页面。* PrintLoopI()有一个用於每个要打印的页面的序例。如果HasMorePages是False,PrintLoop()就会停止。*/return;}}break;case "image"://以下涉及剪切图片int width = image.Width;int height = image.Height;if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)){width = e.MarginBounds.Width;height = image.Height * e.MarginBounds.Width / image.Width;}else{height = e.MarginBounds.Height;width = image.Width * e.MarginBounds.Height / image.Height;}System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(topMargin, leftMargin, width, height);//向画布写入图片for (int i = 0; i < Convert.ToInt32(Math.Floor((double)image.Height / 1600)) + 1; i++){e.Graphics.DrawImage(image, destRect, i * 1600, i * 900, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);//走纸换页if (i * 900 >= e.PageBounds.Height - 60)//页面累加的高度大于页面高度。根据自己需要,可以适当调整{e.HasMorePages = true;printPreviewControl1.Rows += 1;//窗体的打印预览窗口页面自动加1/** PrintPageEventArgs类的HaeMorePages属性为True时,通知控件器,必须再次調用OnPrintPage()方法,打印一个页面。* PrintLoopI()有一个用於每个要打印的页面的序例。如果HasMorePages是False,PrintLoop()就会停止。*/return;}}break;default:break;}//打印完毕后,画线条,且注明打印日期//e.Graphics.DrawLine(new Pen(Color.Black), leftMargin, topMargin, e.MarginBounds.Right, topMargin);//string strdatetime = DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();//e.Graphics.DrawString(string.Format("打印时间:{0}", strdatetime), this.mainFont, Brushes.Black, e.MarginBounds.Right - 240, topMargin + 40, new StringFormat());linesPrinted = 0;e.HasMorePages = false;}/// <summary>  /// EndPrint事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void pdDocument_EndPrint(object sender, PrintEventArgs e){lines = null;image = null;}}
}

C# 图片预览打印方法相关推荐

  1. element upload预览_vue2.0 使用element-ui里的upload组件实现图片预览效果方法

    1.首先我们在cli中引入element-ui 2.然后在具体的代码中放入uoload组件 点击上传 只能上传jpg/png文件,且不超过500kb 3.使用element-ui的upload组件中提 ...

  2. java 预览图片_jquery实现图片预览的方法

    //jquery实现图片预览的方法实例 function viewimg(index) { var name = 'uploadimg' + index; var imgup = $(name); v ...

  3. input file本地图片预览的方法(兼容IE、Chrome和Firefox)

    JS兼容各个浏览器的本地图片上传即时预览效果 2014-02-26      我来说两句       收藏     我要投稿 很早以前 在杭州银行工作曾经碰到这么一个需求,当时也是纠结了很久,也是go ...

  4. vue移动端实现图片预览

    接着上文的文件上传.下载.删除继续往下说,我们先说移动端的图片格式预览 上文说到了vue引用了vant插件,是一个很好用的移动端开发组件,它自带了上传文件的方法,当然也带了图片预览的方法 图片预览其实 ...

  5. java 上传图片,图片预览,2种方法

    java 上传图片,图片预览,2种方法 1. 方法一:将图片上传到特定磁盘 html页面,form表单内容: <div class="layui-form-item"> ...

  6. 简单的实现图片预览, 通过原生ajax以及 jQuery两种方法实现图片预览,有更好的办法可以留言喔...................

    1.原生写ajax实现图片预览: 结构:       <input type="file"> <img src="" alt="&q ...

  7. axios获取图片显示_Vue.js+axios图片预览以及上传显示进度

    在平时项目开发中,我们经常会对文件做一些上传操作,不仅仅要实现基本需求,也要兼顾用户体验,根据自己在工作中遇到的问题谈谈对图片上传的预览以及上传进度的优化. 1.搭建项目 基于Vue.js+axios ...

  8. Vue PDF文件预览打印vue-pdf

    Vue PDF文件预览vue-pdf 最近做项目,遇到预览PDF这个功能,在网上找了找,大多推荐的是pdf.js,不过在Vue中还是想偷懒直接npm组件,最后找到了一个还不错的Vue-pdf 组件,G ...

  9. 使用blob实现文件下载和图片预览

    学习blob对象的笔记 blob叫做二进制大对象(binary large object)主要是存放二进制的容器. 1 存取文本 存放二进制数据,类似数组size为数据长度,type指定类型 exam ...

  10. Vue 组件库 heyui@1.18.0 发布,新增地址选择、图片预览组件

    开发四年只会写业务代码,分布式高并发都不会还做程序员?   新增 CategoryPicker 新增组件 CategoryPicker,地址级联组件的最佳方案. <CategoryPicker ...

最新文章

  1. python引用文件 mode写在后面还是前面,python3 文件操作常用mode参数用法详解
  2. git学习创建项目仓库
  3. 初学者成为优秀Java程序员的8个步骤
  4. SAP Leonardo机器学习图片相似度打分API的测试报告
  5. ajax的学多久,ajax第二天学习
  6. 返回json格式 不忽略null字段
  7. *福音*阿里云redis5.0.*集群搭建及java代码测试详细(终于不用ruby了)
  8. 楼市反弹难以持续 年末房价稳中趋降
  9. 【考研】考研5大分数线——国家线、院校线、自划线、单科线、录取线的区别
  10. asp.net页面加载时间计算
  11. office2010安装提示需要MSXML版本6.10.1129.0
  12. 浙江大学PAT考证,计算机专业的可别错过了,轻松就业+高薪
  13. php背景四周向中间渐变色,使用CSS巧妙地制作背景色渐变动画实例
  14. 震碎认知,将原理融会贯通到顶点的SpringBoot实战项目
  15. python3 selenium模块Chrome设置代理ip的实现
  16. 用计算机亩换算成平方,亩换算平方(平方米换算亩计算器)
  17. 为什么是四次挥手不是三次挥手
  18. ABP VNext学习日记14
  19. 《机器学习》(周志华)线性回归
  20. PTA乙级-1061 判断题-C语言版

热门文章

  1. c语言—数组详解(建议收藏)
  2. 人大金仓数据库高可用集群部署教程
  3. SM2258XT固态硬盘不认盘量产修复开卡工具,SM2258XT开卡教程
  4. linux使用tar命令,Linux的tar命令使用简介
  5. C# WinForm中NotifyICon控件的用法
  6. Inno Setup 详解中文资料
  7. 求助ReportMachine报表,收集ReportMachine资料
  8. 现代高频开关电源技术与应用随堂练习_详细了解开关电源的电磁干扰分析
  9. C语言学习笔记->const和define区别
  10. 奔图m7100dw_奔图M7100dW驱动