作者:沐汐 Vicky
出处:http://www.cnblogs.com/EasyInvoice

一、 问题描述
在页面使用PictureBox 加载资料图片后,点击“打印”,只能打印图片首页,较大图片则无法全部打印。

二、 原因分析
PictureBox中打印图片时没有设置继续打印相关属性,因此每次只能打印第1页。

三、解决方法
PictureBox控件增加打印全部页面属性,如果为True,表示打印c#教程全部页面;如果为False,保留原有逻辑不变。

在打印全部页面时,将控件的图片按页面大小切割,打印页面索引小于页面总数时,设置打印属性PrintPageEventArgs. HasMorePages = true继续打印,打印完成后将该属性设置为False结束打印。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;namespace MyClass
{//public enum OperationState { Default, ZoomIn, ZoomOut };public partial class UCPictureBox : PictureBox{//private OperationState operationState;//处理状态private HScrollBar hScrollBar;//水平滚动条private VScrollBar vScrollBar;//垂直滚动条private PrintDocument printDocument;//打印对象private Rectangle currRect;//现在矩形对象private Bitmap currBmp;//现在图形对象//private int hScrollBarMidVal;//水平滚动条中间值//private int vScrollBarMidVal;//垂直滚动条中间值private RectangleF srcRect;private RectangleF destRect;private bool isMoveScrollBar;//是否移动滚动条       int currentPageIndex = 0;//当前页面int pageCount = 0;//打印页数/// <summary>/// 构造函数/// </summary>public UCPictureBox(){InitializeComponent();SetStyle(ControlStyles.OptimizedDoubleBuffer, true);SetStyle(ControlStyles.AllPaintingInWmPaint, true);//hScrollBarMidVal = 0;//vScrollBarMidVal = 0;           //operationState = OperationState.Default;isMoveScrollBar = false;srcRect = new RectangleF();destRect = new RectangleF();printDocument = new PrintDocument();printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);//构造水平滚动条hScrollBar = new HScrollBar();hScrollBar.Visible = false;hScrollBar.Dock = DockStyle.Bottom;hScrollBar.Scroll += new ScrollEventHandler(scrollBar_Scroll);this.Controls.Add(hScrollBar);//构造垂直滚动条vScrollBar = new VScrollBar();vScrollBar.Visible = false;vScrollBar.Dock = DockStyle.Right;vScrollBar.Scroll +=new ScrollEventHandler(scrollBar_Scroll);this.Controls.Add(vScrollBar);}#region 公共属性[Category("外观"), Description("获取或设置图片")]public new Image Image{get { return base.Image; }set{if (value != null){base.Image = value;currRect.Width = base.Image.Width;currRect.Height = base.Image.Height;hScrollBar.Value = 0;vScrollBar.Value = 0;displayScrollBars();setScrollBarsValues();Invalidate();}}}//缩放比例private int scaleSize = 1;[Category("其它"), Description("获取或设置图片缩放比例")]public Int32 ScaleSize{get { return scaleSize; }set{if (value > 1 && value < 51){scaleSize = value;}}}//缩放倍数private int scaleScope = 5;[Category("其它"), Description("获取或设置图片最大缩放倍数")]public int ScaleScope{get { return scaleScope; }set{if (value > 1 && value < 11){scaleScope = value;}}}//图片边框颜色//private Color borderColor = Color.DarkGray;//[Category("其它"), Description("获取或设置图片边框颜色")]//public Color BorderColor//{//    get { return borderColor; }//    set { borderColor = value; }//}//图片边框宽度private int borderWidth = 5;[Category("其它"), Description("获取或设置图片边框宽度")]public Int32 BorderWidth{get { return borderWidth; }set { borderWidth = value; }}//打印全部页面[Category("其它"),Description("true-打印全部页面,false-打印首页")]public bool PrintAllPages { get; set; }#endregion#region 内部公共方法/// <summary>/// 从新绘制/// </summary>protected override void OnPaint(PaintEventArgs pe){// TODO: 在此处添加自定义绘制代码// 调用基类 OnPaintbase.OnPaint(pe);if (this.Image != null){if (currBmp != null) { currBmp.Dispose(); }currBmp = new Bitmap(currRect.Width + 2 * borderWidth, currRect.Height + 2 * borderWidth);//绘制新图片using (Graphics g = Graphics.FromImage(currBmp)){using (Pen pen = new Pen(BackColor, borderWidth)){g.DrawRectangle(pen, borderWidth * 0.5f, borderWidth * 0.5f,currRect.Width + borderWidth,currRect.Height + borderWidth);}g.DrawImage(this.Image, borderWidth, borderWidth, currRect.Width, currRect.Height);}//图片绘制到控件上pe.Graphics.Clear(BackColor);if (hScrollBar.Visible || vScrollBar.Visible){//滚动条可见drawDisplayedScrollBars(pe.Graphics);}else{//滚动条不可见float x = 0, y = 0;isMoveScrollBar = false;//是否绘制到中心点坐标if (this.SizeMode == PictureBoxSizeMode.CenterImage){x = Math.Abs(ClientSize.Width - currBmp.Width) * 0.5f;y = Math.Abs(ClientSize.Height - currBmp.Height) * 0.5f;}pe.Graphics.DrawImage(currBmp, x, y, currBmp.Width, currBmp.Height);}}}/// <summary>/// 重写控件大小发生改变事件/// </summary>protected override void OnClientSizeChanged(EventArgs e){base.OnClientSizeChanged(e);displayScrollBars();setScrollBarsValues();Invalidate();}#endregion#region 图片打印与预览/// <summary>/// 打印图片/// </summary>public void PrintPictrue(){PrintDialog printDialog = new PrintDialog();printDialog.Document = printDocument;//added by lky 2017-11-16 修复Windows 7 x64位环境无法弹出打印对话框的问题printDialog.UseEXDialog = true;if (printDialog.ShowDialog() == DialogResult.OK){try{printDocument.Print();}catch (Exception ex){MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);return;}}}/// <summary>/// 打印预览/// </summary>public void PrintPreview(){PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();printPreviewDialog.Document = printDocument;try{printPreviewDialog.ShowDialog();}catch (Exception ex){MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);}}/// <summary>/// 打印图片事件/// </summary>private void printDocument_PrintPage(object sender, PrintPageEventArgs e){if (currBmp == null)return;try{if (PrintAllPages)//added by lky 2018-1-15 打印全部页面{int pageWith =(int) e.PageSettings.PrintableArea.Width;int pageHeight = (int)e.PageSettings.PrintableArea.Height;int horizontalTimes = (int)Math.Ceiling((decimal)currBmp.Width / pageWith);//水平方向切割页数int verticalTimes = (int)Math.Ceiling((decimal)currBmp.Height / pageHeight);//垂直方向切割页数if (pageCount == 0){pageCount = horizontalTimes * verticalTimes;//总页数currentPageIndex = 0;}int horizontalCurrentPosition = (currentPageIndex % horizontalTimes);//当前打印的水平偏移页数int verticalCurrentPosition = (int)Math.Floor((decimal)currentPageIndex / horizontalTimes);//当前打印的垂直偏移页数int x = horizontalCurrentPosition * pageWith;//水平方向打印偏移位置int y = verticalCurrentPosition * pageHeight;//垂直方向打印偏移位置int leftX = (currBmp.Width - x) > 0 ? (currBmp.Width - x) : 0;//水平方向未打印尺寸int leftY = (currBmp.Height - y) > 0 ? (currBmp.Height - y) : 0;//垂直方向未打印尺寸Bitmap printBmp = (Bitmap)currBmp.Clone(new Rectangle(x, y, (leftX > pageWith ? pageWith : leftX),(leftY > pageHeight ? pageHeight : leftY)), currBmp.PixelFormat); //待打印图片缓存e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;e.Graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;e.Graphics.DrawImage(printBmp, 0, 0, printBmp.Width, printBmp.Height);printBmp.Dispose();currentPageIndex++;e.HasMorePages = currentPageIndex < pageCount;//是否继续打印if (!e.HasMorePages) pageCount = 0;//打印页数置为0}else //仅打印首页{Bitmap printBmp = (Bitmap)currBmp.Clone();e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;e.Graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;e.Graphics.DrawImage(printBmp, 0, 0, printBmp.Width, printBmp.Height);printBmp.Dispose();}}catch (Exception ex){//写日志文件LogWriter.Write(LOG_CATEGORY.WIN_UI, LOG_LEVEL.ERROR, "UCPicturBox.PrintPage" + ex.ToString());}}#endregion#region 图片放大、缩小、原始大小、全屏显示、旋转功能/// <summary>/// 放大图片/// </summary>public void ZoomInPicture(){if (this.Image != null){//operationState = OperationState.ZoomIn;double scale = 1 + (scaleSize * 0.01);currRect.Width = Convert.ToInt32(currRect.Width * scale);currRect.Height = currRect.Width * this.Image.Height / this.Image.Width;if (currRect.Width < (this.Image.Width * scaleScope)){                   displayScrollBars();setScrollBarsValues();Invalidate();}}}/// <summary>/// 缩小图片/// </summary>public void ZoomOutPicture(){if (this.Image != null){//operationState = OperationState.ZoomOut;double scale = 1 - (scaleSize * 0.01);currRect.Width = Convert.ToInt32(currRect.Width * scale);currRect.Height = currRect.Width * this.Image.Height / this.Image.Width;displayScrollBars();setScrollBarsValues();Invalidate();}}/// <summary>/// 原始大小/// </summary>public void ZoomOriginalPicture(){if (this.Image != null){//operationState = OperationState.Default;isMoveScrollBar = false;currRect.Width = this.Image.Width;currRect.Height = this.Image.Height;displayScrollBars();setScrollBarsValues();Invalidate();}}/// <summary>/// 全屏显示/// </summary>public void ZoomShowAllPicture(){if (this.Image != null){if (this.Image.Width > this.Image.Height){currRect.Width = ClientSize.Width - 2 * borderWidth;currRect.Height = currRect.Width * this.Image.Height / this.Image.Width;if ((currRect.Height + 2 * borderWidth) > ClientSize.Height){currRect.Height = ClientSize.Height - 2 * borderWidth;currRect.Width = currRect.Height * this.Image.Width / this.Image.Height;}//if ((currRect.Height + 2 * borderWidth) > ClientSize.Height)//{//    currRect.Width = ClientSize.Width - 2 * borderWidth - vScrollBar.Width;//    currRect.Height = currRect.Width * this.Image.Height / this.Image.Width;//}}else{currRect.Height = ClientSize.Height - 2 * borderWidth;currRect.Width = currRect.Height * this.Image.Width / this.Image.Height;if ((currRect.Width + 2 * borderWidth) > ClientSize.Width){currRect.Width = ClientSize.Width - 2 * borderWidth;currRect.Height = currRect.Width * this.Image.Width / this.Image.Height;}//if ((currRect.Width + 2 * borderWidth) > ClientSize.Width)//{//    hScrollBar.Value = 0;//    currRect.Height = ClientSize.Height - 2 * borderWidth - hScrollBar.Height;//    currRect.Width = currRect.Height * this.Image.Width / this.Image.Height;//}}isMoveScrollBar = false;displayScrollBars();setScrollBarsValues();Invalidate();}}/// <summary>/// 旋转图片/// </summary>public void RotatePicture(){if (this.Image != null){isMoveScrollBar = false;this.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);int tempWidth = currRect.Width;currRect.Width = currRect.Height;currRect.Height = tempWidth;displayScrollBars();setScrollBarsValues();Invalidate();}}/// <summary>/// 显示水平滚动条与垂直滚动条/// </summary>private void displayScrollBars(){//是否显示水平滚动条if ((currRect.Width + 2 * borderWidth) > ClientSize.Width){ hScrollBar.Visible = true; }else{ hScrollBar.Visible = false; }//是否显示垂直滚动条if ((currRect.Height + 2 * borderWidth) > ClientSize.Height){ vScrollBar.Visible = true; }else{ vScrollBar.Visible = false; }}/// <summary>/// 设置水平滚动条与垂直滚动条值/// </summary>private void setScrollBarsValues(){//设置水平滚动条值if (hScrollBar.Visible){hScrollBar.Minimum = 0;hScrollBar.Maximum = currRect.Width - ClientSize.Width + 2 * borderWidth;hScrollBar.LargeChange = currRect.Width / 10;if (vScrollBar.Visible){hScrollBar.Maximum += vScrollBar.Width;}if (hScrollBar.LargeChange > hScrollBar.Maximum){hScrollBar.LargeChange = hScrollBar.Maximum - 1;}hScrollBar.SmallChange = hScrollBar.LargeChange / 5;hScrollBar.Maximum += hScrollBar.LargeChange;//绘制坐标为中心点if (this.SizeMode == PictureBoxSizeMode.CenterImage){if (hScrollBar.Value == 0 || isMoveScrollBar == false){hScrollBar.Value = (hScrollBar.Maximum - hScrollBar.LargeChange) / 2;}}}else{ hScrollBar.Value = 0; }//设置垂直滚动条值if (vScrollBar.Visible){vScrollBar.Minimum = 0;vScrollBar.Maximum = currRect.Height - ClientSize.Height + 2 * borderWidth;vScrollBar.LargeChange = currRect.Height / 10;if (hScrollBar.Visible){vScrollBar.Maximum += hScrollBar.Height;}if (vScrollBar.LargeChange > vScrollBar.Maximum){vScrollBar.LargeChange = vScrollBar.Maximum - 1;}vScrollBar.SmallChange = vScrollBar.LargeChange / 5;vScrollBar.Maximum += vScrollBar.LargeChange;//绘制坐标为中心点if (this.SizeMode == PictureBoxSizeMode.CenterImage){if (vScrollBar.Value == 0 || isMoveScrollBar ==false){                       vScrollBar.Value = (vScrollBar.Maximum - vScrollBar.LargeChange) / 2;}}}else{ vScrollBar.Value = 0; }}/// <summary>/// 移动水平滚动条事件/// </summary>private void scrollBar_Scroll(object sender, ScrollEventArgs e){isMoveScrollBar = true;using (Graphics graphics = this.CreateGraphics()){drawDisplayedScrollBars(graphics);}this.Update();}/// <summary>/// 从新绘制显示的滚动条/// </summary>private void drawDisplayedScrollBars(Graphics graphics){float x = 0, y = 0;if (this.SizeMode == PictureBoxSizeMode.CenterImage){x = Math.Abs(ClientSize.Width - currBmp.Width - vScrollBar.Width) * 0.5f;y = Math.Abs(ClientSize.Height - currBmp.Height - hScrollBar.Height) * 0.5f;}if (hScrollBar.Visible == false){//水平滚动条不可见destRect.X = x;destRect.Y = 0;srcRect.X = 0;srcRect.Y = vScrollBar.Value;}else if (vScrollBar.Visible == false){//垂直滚动条不可见destRect.X = 0;destRect.Y = y;srcRect.Y = 0;srcRect.X = hScrollBar.Value;}else{//两个滚动条都可见destRect.X = 0;destRect.Y = 0;srcRect.X = hScrollBar.Value;srcRect.Y = vScrollBar.Value;}destRect.Width = currBmp.Width;destRect.Height = currBmp.Height;srcRect.Width = currBmp.Width;srcRect.Height = currBmp.Height;graphics.DrawImage(currBmp, destRect, srcRect, GraphicsUnit.Pixel);}#endregion}
}

以上就是c# winform 解决PictureBox 无法打印全部图片的问题的详细内容

c# winform 解决PictureBox 无法打印全部图片的问题相关推荐

  1. word中图片为嵌入式格式时显示不全_打印Word图片显示不全 Word2007图片显示不全解决方法...

    打印Word图片显示不全 Word2007图片显示不全解决方法,平凡的世界平凡的你,努力学习使我们变得不平凡,今天要介绍的知识是打印Word图片显示不全的相关知识,你准备好学习打印Word图片显示不全 ...

  2. 【最强解决办法】日式打印图片显示不全(word预览却是全的),打印画质下降、转PDF下降等问题

    原文带图片链接:[最强解决办法]打印图片显示不全(word预览却是全的),打印画质下降.转PDF下降等问题 最近写论文,由于学会要求PDF稿内的图片大于300dpi,于是在word那儿折腾了半天. 有 ...

  3. html打印无法打印背景图片,Word/wps怎么打印背景图片?Word背景打印不出来的三种解决办法...

    Word/wps怎么打印背景图片?对于使用WPS工作.学习的朋友们或许会遇到背景无法打印的问题,精美的背景.花纹常常是所做文档的一个不可或缺的部分.那么我们该如何打印出背景呢?小编在这里有多种办法. ...

  4. 【WinForm】打印机编辑打印内容并实现双排打印

    本次使用的打印机是Gprinter GP-9134T 实现打印的主要代码就是 PrintDocument printDocument = new PrintDocument(); //设置边距 pri ...

  5. winform实现pictureBox显示成圆形形状,并实现pictureBox透明

    文章目录 背景 一.将pictureBox变成圆形图片? 二.实现pictureBox透明 三.图片裁剪成圆形 背景 使用winform窗体做一个人脸识别的效果,需要使用三个pictureBox来存放 ...

  6. 将winform的PictureBox/panel控件背景图多余白边设置为透明

    有关如何将winform的PictureBox控件背景图多余白边设置为透明 如图所示: PictureBox控件如果直接这样叠加,原本png图片没有像素的边角会变成白色填充 只需要设置该图片的父容器为 ...

  7. 记一次灰度发版打印背景图片无法加载的处理过程

    记一次灰度发版打印背景图片无法加载的处理过程 前言 需求为给订单加上新的打印模板,测试环境正常,灰度环境打印不出来图片.请求服务器路径可以展示图片,使用lodop设置背景图或打印图片都无法展示 解决过 ...

  8. 关于 打印页面 图片被截断

    当html 页面打印的时候,上下两页出现图片被截断的情况,如下 思路: 需要达到的效果是,当打印一页不够时,整个图片到下一页打印,图片不被截断 解决办法:(.seal包裹每个图章的类) @media ...

  9. 解决Chrome中UEditor插入图片的选择框加载过慢问题

    解决Chrome中UEditor插入图片的选择框加载过慢问题 ../resources/plugins/ueditor/ueditor.all.js 中line24489/24498中的 accept ...

最新文章

  1. 设置IDEA编辑过程直接通过F5刷新网页就可以实时查看JSP文件更新结果,而非通过重新run
  2. net-ldap for ruby openNebula ldap
  3. s5-11 距离矢量路由选择协议
  4. 【ubuntu-anaconda-dlib】undefined symbol: _ZTTNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESa
  5. 2016 - 2- 2 非正式协议与正式协议
  6. java 实体类 时间格式字段注解
  7. 分布式任务调度系统xxl-job
  8. 读书笔记--精通CSS高级Web标准解决方案(一)---CSS基础
  9. 免费试用腾讯云服务器 + nginx建网站
  10. 银行加息有什么影响(央行加息,对股市和房价有何影响?)
  11. 谷歌公布首颗自研手机芯片Tensor
  12. CSS (3) | 盒子
  13. 远程服务器创建文件,ftp创建远程服务器文件夹
  14. Apache service named reported the following error(OS 10055)由于系统缓冲区空间不足或队列已满解决办法?...
  15. centos6.5 安装php探针,Centos5.5下安装LAMP完整版
  16. 10个新技术让明年的科技产品更牛掰
  17. python气泡图画_Python使用Plotly绘图工具,绘制气泡图
  18. 做系统n多年,第一次遇到情况就崩了!
  19. vcruntime140.dll文件丢失的解决方法
  20. 全国计算机一级选择题免费,全国计算机一级考试选择题试题与详细答案(免费).doc...

热门文章

  1. CSS3字体样式及高级特效
  2. 玩转控件DTPicker
  3. zkSnark教程:从方程到验证
  4. 从零开始的openGL--cs游戏(15) Volume阴影。
  5. Java堆内存溢出造成OS卡顿/服务中断的一种情况
  6. QuasarRAT-windows下远程控制工具
  7. 游戏引擎编程需要哪些基本数学知识?
  8. poi获取excel打印标题行与表头,itext生成pdf设置打印标题行与表头
  9. 如何把很多照片拼成一张照片_如何能把多张照片拼凑在一张上图片上
  10. .NET 针对465加密端口 加密协议SSL(Implicit SSL)进行的邮件发送