private string __ImagePath = string.Empty;// "C:\\";//发票图像路径,设置的路径
private string __ImageFullPath = string.Empty;//发票全路径
private Image __InvImgView = null;//存储发票图像
/// <summary>
/// 打印图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrintI_Click(object sender, EventArgs e)
{
this.__ImagePath = Holytax.Common.ConfigXML.InvoiceImagePath;
try
{
bool isSelectInvoice = false;
string thisInvImagePath = string.Empty;
for (int i = 0; i < gridView1.DataRowCount; i++)
{
object obj = gridView1.GetRowCellValue(gridView1.GetRowHandle(i), "COLCHK");
if ((int)obj == 1)
{
object objInvImagePathTp = gridView1.GetRowCellValue(gridView1.GetRowHandle(i), "IMAGEPATH");
if (objInvImagePathTp != null)
{
thisInvImagePath = objInvImagePathTp.ToString();
}
if (thisInvImagePath.Length > 0)
{
arrPrintImageFiles.Add(__ImagePath + thisInvImagePath);
isSelectInvoice = true;
}
}
}
if (isSelectInvoice)
{
PrintImage();
}
else
{
XtraMessageBox.Show("未选择发票或选择的发票找不到图像文件。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception err)
{
LogHelper.Write(err.Message, Holytax.Log.LogHelper.LogMessageType.Fatal, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
XtraMessageBox.Show(err.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#region 图像打印
private void pDocument_PrintPage(object sender, PrintPageEventArgs e)
{
string strSourceFileName = arrPrintImageFiles[i_current].ToString();
try
{
if (!strSourceFileName.StartsWith("http://"))
{
MyImage = Image.FromFile(strSourceFileName);
}
else
{
HttpWebRequest httpRequest = WebRequest.Create(strSourceFileName) as HttpWebRequest;
WebResponse httpResponse = httpRequest.GetResponse();
MyImage = new Bitmap(httpResponse.GetResponseStream());
}
}
catch (Exception err)
{
LogHelper.Write(err.Message, Holytax.Log.LogHelper.LogMessageType.Fatal, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
throw new Exception("找不到图像文件:" + err.Message);
}
MyImage = Common.ComTools.AlterPicResolution(MyImage, (float)(MyImage.Width * (float)2.54 / 24));
e.Graphics.Clip = new Region(e.MarginBounds);
e.Graphics.DrawImageUnscaled(MyImage, e.MarginBounds);
i_current++;
if (i_current < arrPrintImageFiles.Count)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
System.Collections.ArrayList arrPrintImageFiles = new System.Collections.ArrayList();
int i_current = 0;
Image MyImage = null;
private void PrintImage()
{
PrintDocument pDocument = new PrintDocument();
//设置边距
Margins margin = new Margins(100, 0, 100, 0);
pDocument.DefaultPageSettings.Margins = margin;
//横向打印
pDocument.DefaultPageSettings.Landscape = true;
pDocument.DocumentName = "发票图像";
pDocument.PrintPage += new PrintPageEventHandler(this.pDocument_PrintPage);
try
{
//print只能调用一次,e.HasMorePages控制打印的页数
pDocument.Print();
//清空打印所用变量,包括图像,打印页,图片数组
if (MyImage != null)
{
MyImage.Dispose();
MyImage = null;
}
i_current = 0;
arrPrintImageFiles.Clear();
XtraMessageBox.Show("打印完毕.", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pDocument.PrintController.OnEndPrint(pDocument, new PrintEventArgs());
}
}
#endregion
//DevExpress 消息提示
//DevExpress.XtraEditors.XtraMessageBox.Show("打印完毕.", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);