如何在多页中打印一张大图像? [英] How to print one large image in many pages?

查看:54
本文介绍了如何在多页中打印一张大图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to print one tall (long) image in many pages. So in every page, I take a suitable part from the image and I draw it in the page.

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

the problem is that I have got the image shrunk (its shape is compressed) in the page,so I added an scale that its value is 1500 . I think that I can solve the problem if I knew the height of the page (e.Graphics) in pixels. to convert Inches to Pixel, Do I have to multiply by (e.Graphics.DpiX = 600) or multiply by 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);
        }

你能不能让它正常工作.

could you please make it works correctly.

您可以从(此处).

提前致谢.

推荐答案

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

I tried to complete your task. Here you go. Hope it helps.

此方法将图像打印在多页(如果图像较小,则为一页).

This method prints the image on several pages (or one if image is small).

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

This method prints every picture in the List in the needed dimensions (A4 size):

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

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

This method crops the image and returns every part of the image:

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

图像是从图片框加载的,因此请确保图像已加载.(还没有异常处理).

The Image gets loaded from the PictureBox, so make sure the image is loaded. (No exception handling yet).

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

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

These variables are defined as global variables.

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

You can download a sample project here:

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

这篇关于如何在多页中打印一张大图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆