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

这篇文章主要介绍了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 无法打印全部图片的问题

基础教程推荐