Windows 窗体 C# 中的打印面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10605840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print Panel in Windows Form C#
提问by Azeem
I have a situation here. I need to create an employee card structure in windows form using C# in Visual Studio 2010. The structure may contains labels and picture boxes with white background. I had no problem creating it but I am also giving a button named "Print" on this form so that user can print that card. I googled it but nothing concrete stuff found. Please help me out.
我这里有一个情况。我需要在 Visual Studio 2010 中使用 C# 以 windows 形式创建员工卡结构。该结构可能包含标签和白色背景的图片框。我创建它没有问题,但我还在此表单上提供了一个名为“打印”的按钮,以便用户可以打印该卡。我用谷歌搜索,但没有找到具体的东西。请帮帮我。
namespace Employee_Card_Manager
{
public partial class Card : Form
{
public Card(String a, String b, String c, String d, String e, String f, String g, String h, String i)
{
InitializeComponent();
this.label2.Text = a;
this.label9.Text = b;
this.label10.Text = c;
this.label11.Text = d;
this.label12.Text = e;
this.label13.Text = f;
this.label14.Text = g;
this.label16.Text = h;
this.pictureBox1.Image = Image.FromFile(i);
Image myimg = Code128Rendering.MakeBarcodeImage(h, 2, true);
this.pictureBox2.Image = myimg;
}
private void button1_Click(object sender, EventArgs e)
{
//Print Card Code
}
}
}
The card template is as below:
卡片模板如下:


I have placed all card structure on a Panel Control and set Panel background to white. Can you fill the code which will print this card? Thanks
我已将所有卡片结构放在面板控件上,并将面板背景设置为白色。你能填写打印这张卡片的代码吗?谢谢
采纳答案by Azeem
I have found following code which is working perfectly!!
我发现以下代码运行良好!!
//declare event handler for printing in constructor
printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);
//Rest of the code
Bitmap MemoryImage;
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
if (MemoryImage != null)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
base.OnPaint(e);
}
}
void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle pagearea = e.PageBounds;
e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
}
public void Print(Panel pnl)
{
pannel = pnl;
GetPrintArea(pnl);
previewdlg.Document = printdoc1;
previewdlg.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
Print(this.panel1);
}
回答by svenv
Does this MSDN page not work for you? http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx
这个 MSDN 页面不适合你吗? http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx
Edit: I modified the example at aforementioned link and created a PrintablePanel class which you can reuse in various forms:
编辑:我修改了上述链接中的示例并创建了一个 PrintablePanel 类,您可以以各种形式重用它:
public partial class PrintablePanel : Panel
{
public PrintablePanel()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
public Bitmap GetImage()
{
Bitmap memoryImage = null;
Graphics mygraphics = CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
return memoryImage;
}

