如何在许多页面中打印一个大图像?

时间:2012-12-28 21:11:05

标签: c# winforms printing

我想在很多页面上打印一张高(长)图像。因此,在每个页面中,我从图像中选取一个合适的部分,然后在页面中绘制它。

问题是我在页面中缩小了图像(其形状被压缩),所以我添加了一个其值为1500的比例。 如果我知道页面的高度(e.Graphics),我认为我可以解决问题。 要将英寸转换为像素,我必须乘以(e.Graphics.DpiX = 600)或乘以96。

void printdocument_PrintPage(object sender, PrintPageEventArgs e)
        {
           if (pageImage ==  null) 
               return;
            e.Graphics.PageUnit = GraphicsUnit.Pixel;

            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;


            float a = (e.MarginBounds.Width / 100) * e.Graphics.DpiX;
            float b = ((e.MarginBounds.Height / 100) * e.Graphics.DpiY);
            int scale = 1500;
            scale = 0; //try to comment this
            RectangleF srcRect = new RectangleF(0, startY, pageImage.Width, b - scale);
            RectangleF destRect = new RectangleF(0, 0, a, b);
            e.Graphics.DrawImage(pageImage, destRect, srcRect, GraphicsUnit.Pixel);
            startY = Convert.ToInt32(startY + b - scale);
            e.HasMorePages = (startY < pageImage.Height);
        }

请你让它正常工作。

您可以从(here)下载源代码。

先谢谢。

2 个答案:

答案 0 :(得分:2)

我试图完成你的任务。 干得好。希望它有所帮助。

此方法在多个页面上打印图像(如果图像很小,则打印一个图像)。

private void printImage_Btn_Click(object sender, EventArgs e)
    {
        list = new List<Image>();
        Graphics g = Graphics.FromImage(image_PctrBx.Image);
        Brush redBrush = new SolidBrush(Color.Red);
        Pen pen = new Pen(redBrush, 3);
       decimal xdivider = image_PctrBx.Image.Width / 595m;
        int xdiv = Convert.ToInt32(Math.Ceiling(xdivider));
        decimal ydivider = image_PctrBx.Image.Height / 841m;
        int ydiv = Convert.ToInt32(Math.Ceiling(ydivider));
        /*int xdiv = image_PctrBx.Image.Width / 595; //This is the xsize in pt (A4)
        int ydiv = image_PctrBx.Image.Height / 841; //This is the ysize in pt (A4)
        // @ 72 dots-per-inch - taken from Adobe Illustrator

        if (xdiv >= 1 && ydiv >= 1)
        {*/
            for (int i = 0; i < xdiv; i++)
            {
                for (int y = 0; y < ydiv; y++)
                {
                    Rectangle r;
                    try
                    {
                        r = new Rectangle(i * Convert.ToInt32(image_PctrBx.Image.Width / xdiv),
                                                    y * Convert.ToInt32(image_PctrBx.Image.Height / ydiv),
                                                    image_PctrBx.Image.Width / xdiv,
                                                    image_PctrBx.Image.Height / ydiv);
                    }
                    catch (Exception)
                    {
                        r = new Rectangle(i * Convert.ToInt32(image_PctrBx.Image.Width / xdiv),
                          y * Convert.ToInt32(image_PctrBx.Image.Height),
                          image_PctrBx.Image.Width / xdiv,
                          image_PctrBx.Image.Height);
                    }


                    g.DrawRectangle(pen, r);
                    list.Add(cropImage(image_PctrBx.Image, r));
                }
            }

        g.Dispose();
        image_PctrBx.Invalidate();
        image_PctrBx.Image = list[0];

        PrintDocument printDocument = new PrintDocument();
        printDocument.PrintPage += PrintDocument_PrintPage;
        PrintPreviewDialog previewDialog = new PrintPreviewDialog();
        previewDialog.Document = printDocument;
        pageIndex = 0;
        previewDialog.ShowDialog();
        // don't forget to detach the event handler when you are done
        printDocument.PrintPage -= PrintDocument_PrintPage;
    }

此方法以所需尺寸(A4尺寸)打印列表中的每张图片:

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        // Draw the image for the current page index
        e.Graphics.DrawImageUnscaled(list[pageIndex],
                                     e.PageBounds.X,
                                     e.PageBounds.Y);
        // increment page index
        pageIndex++;
        // indicate whether there are more pages or not
        e.HasMorePages = (pageIndex < list.Count);
    }

此方法裁剪图像并返回图像的每个部分:

    private static Image cropImage(Image img, Rectangle cropArea)
    {
        Bitmap bmpImage = new Bitmap(img);
        Bitmap bmpCrop = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.DontCare);
        return (Image)(bmpCrop);
    }

图像从PictureBox加载,因此请确保加载图像。 (尚无异常处理)。

    internal string tempPath { get; set; }
    private int pageIndex = 0;
    internal List<Image> list { get; set; }

这些变量被定义为全局变量。

您可以在此处下载示例项目:

  

http://www.abouchleih.de/projects/PrintImage_multiplePages.zip //旧版本   http://www.abouchleih.de/projects/PrintImage_multiplePages_v2.zip //新

答案 1 :(得分:0)

我创建了一个类文件,用于多页打印单个大图像。

Cls_PanelPrinting.Print Print = new Cls_PanelPrinting.Print(PnlContent / Image);

您必须传递面板或图像。

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

namespace Cls_PanelPrinting
{
    public class Print
    {
        readonly PrintDocument printdoc1 = new PrintDocument();
        readonly PrintPreviewDialog previewdlg = new PrintPreviewDialog();
        public int page = 1;
        internal string tempPath { get; set; }
        private int pageIndex = 0;
        internal List<Image> list { get; set; }
        private int _Line = 0;


        private readonly Panel panel_;

        public Print(Panel pnl)
        {
            panel_ = pnl;
            printdoc1.PrintPage += (printdoc1_PrintPage);
            PrintDoc();
        }


        private void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font myFont = new Font("Cambria", 10, FontStyle.Italic, GraphicsUnit.Point);

            float lineHeight = myFont.GetHeight(e.Graphics) + 4;

            float yLineTop = 1000;



            int x = e.MarginBounds.Left;
            int y = 25; //e.MarginBounds.Top;
            e.Graphics.DrawImageUnscaled(list[pageIndex],
                                        x,y);


            pageIndex++;
            e.HasMorePages = (pageIndex < list.Count);

            e.Graphics.DrawString("Page No: " + pageIndex, myFont, Brushes.Black,
                 new PointF(e.MarginBounds.Right, yLineTop));

        }

        public void PrintDoc()
        {

            try
            {

                Panel grd = panel_;
                Bitmap bmp = new Bitmap(grd.Width, grd.Height, grd.CreateGraphics());
                grd.DrawToBitmap(bmp, new Rectangle(0, 0, grd.Width, grd.Height));

                Image objImage = (Image)bmp;

                Bitmap objBitmap = new Bitmap(objImage, new Size(700, objImage.Height));

                Image PrintImage = (Image)objBitmap;

                list = new List<Image>();
                Graphics g = Graphics.FromImage(PrintImage);
                Brush redBrush = new SolidBrush(Color.Red);
                Pen pen = new Pen(redBrush, 3);
                decimal xdivider = panel_.Width / 595m;
                // int xdiv = Convert.ToInt32(Math.Ceiling(xdivider));
                decimal ydivider = panel_.Height / 900m;
                int ydiv = Convert.ToInt32(Math.Ceiling(ydivider));



                int xdiv = panel_.Width / 595; //This is the xsize in pt (A4)

                for (int i = 0; i < xdiv; i++)
                {
                    for (int y = 0; y < ydiv; y++)
                    {
                        Rectangle r;
                        if (panel_.Height > 900)
                        {

                            try
                            {
                                if (list.Count > 0)
                                {
                                    r = new Rectangle(0, (900 * list.Count), PrintImage.Width, PrintImage.Height - (900 * list.Count));
                                }
                                else
                                {
                                    r = new Rectangle(0, 0, PrintImage.Width, 900);
                                }
                                list.Add(cropImage(PrintImage, r));
                            }

                            catch (Exception)
                            {

                                list.Add(PrintImage);
                            }
                        }
                        else { list.Add(PrintImage); }

                    }
                }

                g.Dispose();

                PrintImage = list[0];


                PrintDocument printDocument = new PrintDocument();
                printDocument.PrintPage += printdoc1_PrintPage;
                PrintPreviewDialog previewDialog = new PrintPreviewDialog();
                previewDialog.Document = printDocument;
                pageIndex = 0;

                printDocument.DefaultPageSettings.PrinterSettings.PrintToFile = true;

                string path = "d:\\BillIng.xps";
                if (File.Exists(path))
                    File.Delete(path);

                printDocument.DefaultPageSettings.PrinterSettings.PrintFileName = "d:\\BillIng.xps";
                printDocument.PrintController = new StandardPrintController();


                printDocument.Print();


                printDocument.PrintPage -= printdoc1_PrintPage;
            }

            catch { }

        }
        private static Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.DontCare);
            return (Image)(bmpCrop);
        }

    }
}