110,534
社区成员
发帖
与我相关
我的任务
分享
public partial class print : Form
{
public print()
{
InitializeComponent();
this.printDocument1.OriginAtMargins = true;//启用页边距
this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
}
private void button3_Click(object sender, EventArgs e) //打印设置
{
this.pageSetupDialog1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e) //打印
{
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
private void button1_Click(object sender, EventArgs e) //打印预览
{
printDialog1.Document = printDocument1;
this.printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
////打印内容 为 整个Form
//Image myFormImage;
//myFormImage = new Bitmap(this.Width, this.Height);
//Graphics g = Graphics.FromImage(myFormImage);
//g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
//e.Graphics.DrawImage(myFormImage, 0, 0);
//打印内容 为 局部的 this.groupBox1
Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
////打印内容 为 自定义文本内容
//Font font = new Font("宋体", 12);
//Brush bru = Brushes.Blue;
//for (int i = 1; i <= 5; i++)
//{
// e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
//}
}