0

我想在单页上打印两个图像。我试过下面的代码,但它在不同的页面上打印所有图像。

 public void PD_PrintPage(object sender, PrintPageEventArgs e)
    {

        float W = e.MarginBounds.Width;

        float H = e.MarginBounds.Height;

        for (; FileCounter >= 0; FileCounter--)
        {

            try
            {

                Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);

                if (Bmp.Width / W < Bmp.Height / H)

                    W = Bmp.Width * H / Bmp.Height;

                else
                    H = Bmp.Height * W / Bmp.Width;

                e.Graphics.DrawImage(Bmp, 0, 0, W, H);

                break;

            }

            catch
            {

            }

        }

        FileCounter -= 1;

        if (FileCounter > 0)
        {

            e.HasMorePages = true;

        }

        else
        {

            FileCounter = BmpFiles.Length - 1;

        }

    }

这将打印不同页面中的所有图像

我想要一些功能,可以打印一张图像,留出一些空间,如果有空间,再在同一页打印其他图像。

4

3 回答 3

1

在您的代码中,您每页只打印一张图像,因为您在try. 如果可以只打印一张图像(第二张图像没有足够的空间)或两张图像(你达到了你想要的),你应该根据决定动态地离开循环,而不是使用没有条件的中断。

    //for-loop for printing maximum two images as long as there are files to print
    for (int i = 0; i < 2 && FileCounter >= 0; i++)
    {

        //here comes your printing code just indicated with the draw-call

        e.Graphics.DrawImage(Bmp, 0, 0, W, H);

        //after a image was printed decrement your filecounter
        FileCounter --;

        //after a image was drawn check if there is enough space for the next image
        //if there is not enough space leave the loop with break
        if(condition)
             break;
    }

目前我没有足够的声誉来评论这个页面上的东西......所以:永远不要像“Sayka”在他的回答中建议的那样使用“goto”。那是非常糟糕的风格和编码

于 2013-09-05T08:12:02.670 回答
0

printDocument 的工作原理是这样的:首先程序到达 printPage 函数,读取所有代码,在函数的最后,如果存在一行 e.hasMorePages = true; ,然后程序从头开始重新进入函数并再次读取代码以将其打印到下一页并继续直到它读取一行 e.hasMorepages = false .. 所以你不必在函数内放置循环。您要做的是在类中创建变量,并在打印作业完成后增加或减少变量以使满足 e.hasMorePages = false 的条件..

public void PD_PrintPage(object sender, PrintPageEventArgs e)
{

    float W = e.MarginBounds.Width;

    // if you are calculating the whole page margin, then split it to carry 2 images

    float H = e.MarginBounds.Height / 2;

    // for space btwn images

    H -= 5.0;

    // First Image
        try
        {

            Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);

            if (Bmp.Width / W < Bmp.Height / H)

                W = Bmp.Width * H / Bmp.Height;

            else
                H = Bmp.Height * W / Bmp.Width;

            e.Graphics.DrawImage(Bmp, 0, 0, W, H);

            break;

        }

        catch
        {

        }

      FileCounter --;
      if (FileCounter < 0) goto goDaddy;

     //Second Img
        try
        {

            Bitmap Bmp = new Bitmap(BmpFiles[FileCounter]);

            if (Bmp.Width / W < Bmp.Height / H)

                W = Bmp.Width * H / Bmp.Height;

            else
                H = Bmp.Height * W / Bmp.Width;

            e.Graphics.DrawImage(Bmp, 0, H + 2.5, W, H);

            break;

        }

        catch
        {

        }        

    FileCounter --;

 goDaddy:;
    e.HasMorePages  = (FileCounter >= 0)


}

我没有检查代码,只是试图向您展示这个概念..

于 2013-09-05T06:19:45.433 回答
0

我发现它工作得非常好,不会失去我使用内存流体验过的任何图像质量。

 private void printBothGraphs_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog custom = new PrintPreviewDialog();
        custom.ClientSize = new Size(1000, 750);

        System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();


        pd.DefaultPageSettings.Color = true;

        pd.PrintPage += new PrintPageEventHandler(pd_PrintPageBoth);

        custom.Document = pd;


        custom.ShowDialog();
    }
    private void pd_PrintPageBoth(object sender, PrintPageEventArgs ev)
    {
        // Create and initialize print font 
        System.Drawing.Font printFont = new System.Drawing.Font("Arial", 10);
        ev.PageSettings.Color = true;
        // Create Rectangle structure, used to set the position of the chart Rectangle 
        Rectangle myRec = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Y, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
        Rectangle myRec2 = new System.Drawing.Rectangle(ev.MarginBounds.X, ev.MarginBounds.Height / 2 + 90, ev.MarginBounds.Width, ev.MarginBounds.Height / 2);
        //dataChart and outputGraph are two mscharts in my form
        dataChart.Printing.PrintPaint(ev.Graphics, myRec);
        outputGraph.Printing.PrintPaint(ev.Graphics, myRec2);
    }
于 2014-07-11T18:53:54.377 回答