这篇文章主要介绍了c# winform 解决PictureBox 无法打印全部图片的问题,帮助大家更好进行c# winform开发,感兴趣的朋友可以了解下
作者:沐汐 Vicky
出处:http://www.cnblogs.com/EasyInvoice
一、 问题描述
在页面使用PictureBox 加载资料图片后,点击“打印”,只能打印图片首页,较大图片则无法全部打印。
二、 原因分析
PictureBox中打印图片时没有设置继续打印相关属性,因此每次只能打印第1页。
三、解决方法
PictureBox控件增加打印全部页面属性,如果为True,表示打印全部页面;如果为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; }
/
沃梦达教程
本文标题为:c# winform 解决PictureBox 无法打印全部图片的问题
基础教程推荐
猜你喜欢
- C语言数组长度的计算方法实例总结(sizeof与strlen) 2023-04-26
- 05-C语言进阶——动态内存管理 2023-11-20
- C语言植物大战数据结构二叉树递归 2023-04-09
- g++: const 丢弃限定符 2022-10-07
- Qt数据库应用之实现通用数据库请求 2023-03-18
- 利用QT设计秒表功能 2023-05-30
- character-encoding – Linux中最常见的C语言编码(和Unix?) 2023-11-21
- C语言的三种条件判断语句你都了解吗 2023-03-05
- VisualStudio2010安装教程 2023-01-05
- 纯C++代码详解二叉树相关操作 2023-05-15
