如何在多页中打印一张大图

本文关键字:一张 打印 | 更新日期: 2023-09-27 17:49:57

我想在多个页面中打印一个高(长)图像。因此,在每一页中,我从图像中选取合适的部分并将其绘制在页面中。

问题是我在页面中缩小了图像(它的形状被压缩了),所以我添加了一个比例,它的值是1500。我想如果我知道页面(如图形)的高度(以像素为单位),我就可以解决这个问题了。要将英寸转换为像素,我必须乘以(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);
        }

你能让它正常工作吗

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

如何在多页中打印一张大图

我尽力完成你的任务。给你。希望能有所帮助。

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

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//NEW

我已经创建了一个用于多页打印单个大图的class文件。


Cls_PanelPrinting。Print Print =new cls_panelprint .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);
        }
    }
}