C#列印圖片

來源:互聯網
上載者:User

標籤:fromfile   image   phi   from   end   str   bounds   oid   current   

列印的原理是:產生mdi檔案,系統碰到mdi的時候會自動以列印的方式處理。所以,不管用什麼模板,什麼方式;能在PrintPage事件處理中,產生一張要列印內容的圖片就OK了!

C#實現列印源碼如下:

#region 列印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            //預覽列印
            //PrintPreviewDialog ppd = new PrintPreviewDialog();
            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 = lblDate.Text; //當前日期
            string flowId = lblFlowId.Text; //流水號
            string payDate = PayDate.Year.ToString() + "年" + PayDate.Month.ToString() + "月"; //應收年月
            string adminId = lblAdminId.Text; //操作員編號
            string baseExpense = lblBaseExpense.Text; //應交基本費用
            string fine = lblFine.Text; //罰款數目
            string upExpense = lblUpExpense.Text; //上月上餘
            string actualExpense = txtActualExpense.Text; //實際應交費用
            string chineseExpense = DecimalToChinese.ConvertSum(actualExpense); //實際應交費用的中文大寫

   //讀取圖片模板
            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);
        }

        /// <summary>
        /// 將收費結果填充到圖片模板
        /// </summary>
        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);
            
   //填充資料到圖片模板(位置要在製作圖片模板的時候度量好)
            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 

轉自:http://hi.baidu.com/zhaogaoyan8/blog/item/d50fbf9a3f66c0bdc9eaf4c7.html

C#列印圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.