打印控件 [英] Printing a control
问题描述
我想知道是否有一种简单的方法可以将C#中的任何控件输出到打印机。我的具体示例是尝试将TableLayoutPanel打印到收据上(这样我就不必担心分页符或其他问题),但是我希望能够打印出发送给我的任何可见对象。现在,我必须先创建一个位图,然后执行TableLayoutPanel.DrawToBitmap,但这似乎效率很低,并且由于我已经有Graphics对象可以进行打印,因此应该有一种简单的方法来进行此操作。谢谢!
编辑:我注意到有一个 ControlPaint.Draw__,但是它没有很多控件可以绘制(它具有Border,Button,CheckBox,ComboBox )..
private static void PrintControl(Control control)
{
var bitmap = new Bitmap(control.Width,control.Height);
control.DrawToBitmap(位图,新Rectangle(0,0,control.Width,control.Height));
var pd = new PrintDocument();
pd.PrintPage + =(s,e)=> e.Graphics.DrawImage(bitmap,100,100);
pd.Print();
}
它仍在使用DrawToBitmap,但最优雅。 / p>
它非常简洁,可读且效率不高,所以我看不出有什么理由不喜欢它。
I was wondering if there is an easy way to print out any control in C# to a printer. My specific example is trying to print a TableLayoutPanel to a receipt (so i don't have to worry about page breaks or anything), but I would like the ability to print out any visible object that is sent to me. Right now I have to create a bitmap, and then do a TableLayoutPanel.DrawToBitmap, but this seems very inefficient and since I already have the Graphics object for printing, there should be an easy way to do this.. Thanks!
Edit: I have noticed that there's a "ControlPaint.Draw__", however it doesn't have a lot of controls it is able to draw (it has Border, Button, CheckBox, ComboBox)..
private static void PrintControl(Control control)
{
var bitmap = new Bitmap(control.Width, control.Height);
control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
var pd = new PrintDocument();
pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 100, 100);
pd.Print();
}
It's still using DrawToBitmap, but it's most elegant you're gonna get.
It is pretty concise, readable and not inefficient, so I don't see any reason not to like it.
这篇关于打印控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!