{
#region 打印
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
if (picBox.Image == null)
{
Auda.Form.Class.MessageBoxA.MsgBoxWarning(null, "请先选择你要打印的附件!");
return;
}
PrintDocument pd = new PrintDocument();
//设置边距
Margins margin = new Margins(20, 20, 20, 20);
pd.DefaultPageSettings.Margins = margin;
纸张设置默认
//PaperSize pageSize = new PaperSize("First custom size", 800, 600);
//pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
ppd.Document = pd;
ppd.ShowDialog();
try
{
// pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
}
}
//打印事件处理
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
string date = DateTime.Now.ToString(); //当前日期
string name = "姓名";
//读取图片
Image temp = picBox.Image;
GetResultIntoImage(ref temp,name,date);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = temp.Width;
int height = temp.Height;
Rectangle destRect = new Rectangle(x, y, width, height);
e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
}
/// <summary>
/// 将结果填充到图片
/// </summary>
private void GetResultIntoImage(ref Image temp, string name, string date)
{
//读取图片
Graphics g = Graphics.FromImage(temp);
Font f = new Font("宋体", 12);
Brush b = new SolidBrush(Color.Black);
//填充数据到图片
g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
g.DrawString(name, f, b, 168, 105);
g.DrawString(date, f, b, 176, 269);
g.Dispose();
}
#endregion