C# 實(shí)現(xiàn)圖片打印

打印的原理是:生成mdi文件,系統(tǒng)碰到mdi的時(shí)候會(huì)自動(dòng)以打印的方式處理。所以,不管用什么模板,什么方式;能在PrintPage事件處理中,生成一張要打印內(nèi)容的圖片就OK了!

C#實(shí)現(xiàn)打印源碼如下:

#region 打印

? ? ? ?private void btnPrint_Click(object sender, EventArgs e)

? ? ? ?{

? ? ? ? ? ?//打印預(yù)覽

? ? ? ? ? ?//PrintPreviewDialog ppd = new PrintPreviewDialog();

? ? ? ? ? ?PrintDocument pd = new PrintDocument();

? ? ? ? ? ?//設(shè)置邊距

? ? ? ? ? ?Margins margin = new Margins(20, 20, 20, 20);

? ? ? ? ? ?pd.DefaultPageSettings.Margins = margin;

? ? ? ? ? ?////紙張?jiān)O(shè)置默認(rèn)

? ? ? ? ? ?//PaperSize pageSize = new PaperSize("First custom size", 800, 600);

? ? ? ? ? ?//pd.DefaultPageSettings.PaperSize = pageSize;

? ? ? ? ? ?//打印事件設(shè)置

? ? ? ? ? ?pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

? ? ? ? ? ?//ppd.Document = pd;

? ? ? ? ? ?//ppd.ShowDialog();

? ? ? ? ? ?try

? ? ? ? ? ?{

? ? ? ? ? ? ? ?pd.Print();

? ? ? ? ? ?}

? ? ? ? ? ?catch (Exception ex)

? ? ? ? ? ?{

? ? ? ? ? ? ? ?MessageBox.Show(ex.Message, "打印出錯(cuò)", MessageBoxButtons.OK, MessageBoxIcon.Error);

? ? ? ? ? ? ? ?pd.PrintController.OnEndPrint(pd, new PrintEventArgs());

? ? ? ? ? ?}

? ? ? ?}

? ? ? ?//打印事件處理

? ? ? ?private void pd_PrintPage(object sender, PrintPageEventArgs e)

? ? ? ?{

? ? ? ? ? ?string date = lblDate.Text; //當(dāng)前日期

? ? ? ? ? ?string flowId = lblFlowId.Text; //流水號(hào)

? ? ? ? ? ?string payDate = PayDate.Year.ToString() + "年" + PayDate.Month.ToString() + "月"; //應(yīng)收年月

? ? ? ? ? ?string adminId = lblAdminId.Text; //操作員編號(hào)

? ? ? ? ? ?string baseExpense = lblBaseExpense.Text; //應(yīng)交基本費(fèi)用

? ? ? ? ? ?string fine = lblFine.Text; //罰款數(shù)目

? ? ? ? ? ?string upExpense = lblUpExpense.Text; //上月上余

? ? ? ? ? ?string actualExpense = txtActualExpense.Text; //實(shí)際應(yīng)交費(fèi)用

? ? ? ? ? ?string chineseExpense = DecimalToChinese.ConvertSum(actualExpense); //實(shí)際應(yīng)交費(fèi)用的中文大寫

? //讀取圖片模板

? ? ? ? ? ?Image temp = Image.FromFile(@"Receipts.jpg");

? ? ? ? ? ?GetResultIntoImage(ref temp, UserId, flowId, date, baseExpense, fine, upExpense, actualExpense, chineseExpense, payDate, adminId);

? ? ? ? ? ?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);

? ? ? ?}

? ? ? ?///

/// 將收費(fèi)結(jié)果填充到圖片模板

///

private void GetResultIntoImage(

ref Image temp,

string userId,

string flowId,

string currentDate,

string baseExpense,

string fine,

string upExpense,

string actualExpense,

string chineseExpense,

string payDate,

string adminName)

{

//讀取圖片模板

Graphics g = Graphics.FromImage(temp);

Font f = new Font("宋體", 12);

Brush b = new SolidBrush(Color.Black);


//填充數(shù)據(jù)到圖片模板(位置要在制作圖片模板的時(shí)候度量好)

g.DrawImage(temp, 0, 0, temp.Width, temp.Height);

g.DrawString(userId, f, b, 168, 105);

g.DrawString(UserName, f, b, 166, 134);

g.DrawString(flowId, f, b, 535, 105);

g.DrawString(currentDate, f, b, 535, 134);

g.DrawString(baseExpense, f, b, 219, 202);

g.DrawString(fine, f, b, 372, 202);

g.DrawString(upExpense, f, b, 486, 202);

g.DrawString(actualExpense, f, b, 596, 202);

g.DrawString(chineseExpense, f, b, 196, 238);

g.DrawString(payDate, f, b, 176, 269);

g.DrawString(adminName, f, b, 497, 298);

g.Dispose();

}

#endregion

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容