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

C# 如何使用copyfromscreen捕获的多个屏幕截图进行打印,c#,winforms,printing,C#,Winforms,Printing,我对C#相当陌生,但我终于有了我的第一个程序并开始运行,我需要把它打印出来。它是一个窗口窗体,在不同的选项卡控件上显示信息和计算,有点像Excel。当前正在查看的页面使用copyfromscreen方法可以很好地打印,但我无法获得正确打印的其他页面。我有大约20个标签,我想能够打印一次。我找到了一种将控件内容打印到文本文件的方法,但我更希望能够打印表单的外观。谢谢 Bitmap memoryImage; Bitmap memoryImage2; private void

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

尝试改用
TabPage
DrawToBitmap
方法:

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);
}
要获取
选项卡页面的所有图像,可以进行如下循环:

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
列表图像=新列表();
私有无效捕获屏幕(){
foreach(tabControlMain.TabPages中的TabPage页){
位图bm=新位图(page.Width、page.Height);
tabControlMain.SelectedTab=页面;
第页DrawToBitmap(bm,第页ClientRectangle);
图像。添加(bm);
}
}
//然后,您可以访问图像列表中选项卡页面的图像
//TabPage的索引与列表图像中的图像索引相对应

尝试使用
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);
}
要获取
选项卡页面的所有图像,可以进行如下循环:

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
列表图像=新列表();
私有无效捕获屏幕(){
foreach(tabControlMain.TabPages中的TabPage页){
位图bm=新位图(page.Width、page.Height);
tabControlMain.SelectedTab=页面;
第页DrawToBitmap(bm,第页ClientRectangle);
图像。添加(bm);
}
}
//然后,您可以访问图像列表中选项卡页面的图像
//TabPage的索引与列表图像中的图像索引相对应

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

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

首先,您应该使用
打印文档
打印预览对话框
对象执行打印相关任务,并使用事件处理程序执行打印。 其次,您需要对代码进行一些优化,以下是解决方案:

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

仅捕获
选项卡页面
是否可以?或者您需要捕获整个
TabControl
?是的,我只需要能够使用PrintPageEventArgs.HasMorePages属性打印20个左右的tabPages中的每一个就可以打印多个页面。MSDN库中关于PrintDocument的文章很好地介绍了这一点。只需数数页数,选择正确的图形即可。您还需要BeginPrint事件来重置计数器。请注意屏幕截图很难看,打印机的分辨率比屏幕的分辨率高得多,因此每个屏幕像素都会在纸上变成一个6x6的斑点。仅捕获
选项卡页
可以吗?或者您需要捕获整个
TabControl
?是的,我只需要能够使用PrintPageEventArgs.HasMorePages属性打印20个左右的tabPages中的每一个就可以打印多个页面。MSDN库中关于PrintDocument的文章很好地介绍了这一点。只需数数页数,选择正确的图形即可。您还需要BeginPrint事件来重置计数器。请注意,屏幕截图很难看,打印机的分辨率远高于屏幕的分辨率,因此每个屏幕像素都会在纸上变成一个6x6的斑点。您需要使用BeginPrint才能将首页设置为true。您需要使用BeginPrint才能将首页设置为true。