【问题标题】:print two images on single page c#在单页上打印两个图像c#
【发布时间】:2013-09-05 05:06:02
【问题描述】:

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

 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;

        }

    }

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

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

【问题讨论】:

    标签: c# winforms printing


    【解决方案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”。这真是糟糕的风格和编码

    【讨论】:

    • 如何检查下一张图片是否有足够的空间
    • 您在 EventArguments 中有一个 PageBounds 属性。这些代表页面的总面积。你也知道你的形象的高度。其他一切都只是数学。请记住,0;0 在左上角!使用属性的高度(或 y)并用一些偏移量减去图像的高度,以在图像之间留出空间。然后检查剩余区域的高度是否足以容纳您的图像。另外,您可能应该在页面的边框上留一些空间。
    • 通常创建打印方法会导致尝试一切是否符合要求。这并不好,但这是正确放置事物的唯一方法。使用 pdf 打印机(如 freepdf)对此有帮助;)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-16
        • 2015-02-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多