1

我对 C# 很陌生,但我终于启动并运行了我的第一个程序,我需要让它打印。它是一个窗口表单,在不同的选项卡控件上包含信息和计算,有点像 Excel。使用 copyfromscreen 方法可以正常打印当前正在查看的页面,但我无法正确打印其他页面。我想一次打印大约 20 个标签。我找到了一种将控件内容打印到文本文件中的方法,但我更希望能够打印表单的样子。谢谢。

    Bitmap memoryImage;
    Bitmap memoryImage2;
    private void CaptureScreen()
    {

        Graphics myGraphics = this.CreateGraphics();
        Size s = tabControlMain.Size;
        s.Width = s.Width + 20;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X+15, this.Location.Y+80, 0, 0, s);

        tabControlMain.SelectedIndex = 1;
        memoryImage2 = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics2 = Graphics.FromImage(memoryImage2);
        memoryGraphics2.CopyFromScreen(this.Location.X + 15, this.Location.Y + 80, 0, 0, s);


    }
    private void printDocumentReal_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
        e.Graphics.DrawImage(memoryImage2, 0, 550);

    }
    private void printToolStripButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocumentReal.Print();
    }
4

2 回答 2

1

首先,您应该使用PrintDocumentPrintPreviewDialog对象来完成与打印相关的任务,并使用一个事件处理程序来进行打印。 其次,您需要对您的代码进行一些优化,这是解决方案:

private void printToolStripButton_Click(object sender, EventArgs e)
    {
        PrintDocument document = new PrintDocument();
        document.PrintPage += new PrintPageEventHandler(document_PrintPage);
        PrintPreviewDialog preview = new PrintPreviewDialog() { Document = document };
        // you will be able to preview all pages before print it ;)
        try
        {
            preview.ShowDialog();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\nYou need to install a printer to preform print-related tasks!", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    Boolean firstPage = true;
    private void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        if (firstPage)
        {
            tabControlMain.SelectTab(0);
            firstPage = false;
        }
        Graphics g = e.Graphics;
        TabPage tab = tabControlMain.SelectedTab;
        using (Bitmap img = new Bitmap(tab.Width, tab.Height))
        {
            tab.DrawToBitmap(img, tab.ClientRectangle);
            g.DrawImage(img, new Point(e.MarginBounds.X, e.MarginBounds.Y)); // MarginBounds means the margins of the page 
        }
        if (tabControlMain.SelectedIndex + 1 < tabControlMain.TabCount)
        {
            tabControlMain.SelectedIndex++;
            e.HasMorePages = true;//If you set e.HasMorePages to true, the Document object will call this event handler again to print the next page.
        }
        else
        { 
            e.HasMorePages = false;
            firstPage = true;
        } 
    }

我希望它和你一起工作正常如果你需要将所有选项卡保存为硬盘上的一组图像,这里还有一个:

    public void RenderAllTabs()
    {
        foreach (TabPage tab in tabControlMain.TabPages)
        {
            tabControlMain.SelectTab(tab);
            using (Bitmap img = new Bitmap(tab.Width, tab.Height))
            {
                tab.DrawToBitmap(img, tab.ClientRectangle);
                img.Save(string.Format(@"C:\Tabs\{0}.png", tab.Text));
            }
        }
    }
于 2013-08-18T12:26:45.687 回答
0

尝试使用以下DrawToBitmap方法TabPage

private void CaptureScreen()
{
    memoryImage = new Bitmap(tabControlMain.SelectedTab.Width, tabControlMain.SelectedTab.Height);
    tabControlMain.SelectedTab.DrawToBitmap(memoryImage, tabControlMain.SelectedTab.ClientRectangle);
    tabControlMain.SelectedIndex = 1;
    memoryImage2 = new Bitmap(tabControlMain.SelectedTab.Width, tabControlMain.SelectedTab.Height);        
    tabControlMain.SelectedTab.DrawToBitmap(memoryImage2, tabControlMain.SelectedTab.ClientRectangle);
}

要获取您的所有图像TabPages,您可以进行如下循环:

List<Bitmap> images = new List<Bitmap>();
private void CaptureScreen(){
   foreach(TabPage page in tabControlMain.TabPages){
      Bitmap bm = new Bitmap(page.Width, page.Height);
      tabControlMain.SelectedTab = page;
      page.DrawToBitmap(bm, page.ClientRectangle);
      images.Add(bm);
   }
}
//Then you can access the images of your TabPages in the list images
//the index of TabPage is corresponding to its image index in the list images
于 2013-08-18T07:04:23.270 回答