2

我想在多页中打印一张高(长)图像。因此,在每一页中,我都会从图像中提取合适的部分,然后将其绘制在页面中。

问题是我在页面中缩小了图像(其形状被压缩),所以我添加了一个值为 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);
        }

你能不能让它正常工作。

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

提前致谢。

4

2 回答 2

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 尺寸)打印 List 中的每张图片:

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

于 2012-12-28T23:36:32.077 回答
0

我已经为多页创建了一个类文件打印一个大图像。


Cls_PanelPrinting.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);
        }

    }
}
于 2017-10-16T11:12:00.357 回答