C#中的打印功能 [英] Print function in C#

查看:98
本文介绍了C#中的打印功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想添加打印功能以打印选项卡页面上显示的所有内容(少量图表).


我找到了这段代码,应该将面板转换为位图然后再打印.

但是它只打印一张空白纸.

我在做什么错了?

Hello Everyone,

I want to add a print function to print everything( few graphs) that is displayed on tabpage.


I found this code, that supposed to convert the panel to bitmap and then print it.

however it just prints a blank sheet.

What is wrong with what im doing?

Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));
PrintDialog MyPrintDialog = new PrintDialog();
if (MyPrintDialog.ShowDialog() == DialogResult.OK)
    {
         System.Drawing.Printing.PrinterSettings values;
         values = MyPrintDialog.PrinterSettings;
         MyPrintDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
         MyPrintDocument.Print();
    }
 MyPrintDocument.Dispose();

推荐答案

您可以使用
Visual Basic Power Packs PrintForm组件
PrintForm.Print()方法
或使用GDI +
You can use
Visual Basic Power Packs PrintForm Component
PrintForm.Print() method
or use GDI+
Printing using GDI+ : a few tips[^]


您要做的只是调用Print事件, PrintDocument,而没有定义应该处理Print事件的方法.您应该在if块中的代码中添加以下内容:-
All you are doing is calling the Print event of the PrintDocument, without defining what method should handle the Print event. You should add something like this to your code in the if block:-
MyPrintDocument.PrintPage += new PrintPageEventHandler(PrintPage);


然后定义一个方法PrintPage实际执行打印操作


then you define a method PrintPage which actually does the printing

void PrintPage(object sender, PrintPageEventArgs e)
{
     Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);  
     panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));  
     e.Graphics.DrawImage(MyChartPanel, newPoint(0,0));
}



希望对您有帮助



Hope this helps


这篇关于C#中的打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆