在单页c#上打印两张图像

时间:2013-09-05 05:06:03

标签: c# winforms printing

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

 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;

        }

    }

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

我想要一些能够打印一张图像的功能,留出一些空间,如果剩余空间则再次在同一页面中显示其他图像。

3 个答案:

答案 0 :(得分:1)

在你的代码中,每页只有一个图像,因为你在try的末尾留下了带有break语句的循环。如果没有条件你可以根据决定动态地离开循环,如果有可能只打印一张图像(第二张图像没有足够的空间)两个图像(你实现了你想要的)。

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

目前我没有足够的声誉来评论此页面上的某些内容...所以:永远不要使用'goto'作为“Sayka”在他的回答中提出的建议。这是非常糟糕的风格和编码

答案 1 :(得分: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)


}

我没有检查过代码,只是试着向你展示这个概念..

答案 2 :(得分: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);
    }