如何使用copyfromscreen捕获的多个屏幕截图进行打印

本文关键字:屏幕截图 打印 何使用 copyfromscreen | 更新日期: 2023-09-27 18:28:00

我对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();
    }

如何使用copyfromscreen捕获的多个屏幕截图进行打印

首先,应该使用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));
            }
        }
    }

尝试使用TabPageDrawToBitmap方法:

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