问题描述:

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.

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.

you can download the source code from (here).

thanks in advanced.

网友答案:

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

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 // OLD Version http://www.abouchleih.de/projects/PrintImage_multiplePages_v2.zip // NEW

相关阅读:
Top