안녕하세요 DKLEE(이동규) 입니다.
이번 포스팅에서는 C# 에서의 인쇄기능을 예제를 통해 알아보겠습니다. 인쇄 미리보기를 통해 작업한 내용을 확인해보고 인쇄까지 작동하는 방법을 아래 코드를 통해 확인해 볼 수 있습니다.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace ImagePrinting_ex01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument docToPrint = new PrintDocument();
PageSettings ps = new PageSettings();
ps.Margins = new Margins(10, 10, 10, 10);
docToPrint.DefaultPageSettings = ps;
PrintPreviewDialog pd = new PrintPreviewDialog();
pd.ClientSize = new Size(500, 500);
pd.UseAntiAlias = true;
docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
pd.Document = docToPrint;
pd.Show();
}
void docToPrint_PrintPage(object sender, PrintPageEventArgs e)
{
Font pf = new Font("궁서", 20, FontStyle.Regular);
Image img = Image.FromFile(@"..\..\test.png");
e.Graphics.DrawImage(img, 0, 0, 500, 500);
string str = "My name is Dongkyu Lee";
e.Graphics.DrawString(str, pf, Brushes.Black, 10, 500);
}
}
}
아래 그림은 실행되고 있는 모습입니다. 추가한 이미지와 글씨가 바르게 나옴을 확인해 볼 수 있습니다. 인쇄 미리 보기가 실행되며 그안에서 인쇄버튼 / 확대 / 보기방법 을 실행할 수 있습니다.

ImagePrinting_ex01_1.zip




