📁 [4] 개발자 정보 & 코드 노트/C#
C# Windows Forms 강의 94편: QR 코드 생성 애플리케이션 제작 - ZXing.Net 활용
wawManager
2025. 5. 8. 12:00
1. 강의 개요
이번 강의에서는 ZXing.Net 라이브러리를 활용하여 QR 코드를 생성하는 애플리케이션을 제작합니다.
QR 코드는 정보를 저장하고 공유하는 데 널리 사용되는 2D 바코드 형식으로,
이 강의에서는 입력된 텍스트를 QR 코드로 변환하고 이미지를 저장하는 기능을 구현합니다.
2. 학습 목표
- ZXing.Net 라이브러리를 사용해 QR 코드 생성
- 사용자 입력을 기반으로 QR 코드를 이미지로 저장
- PictureBox를 활용한 QR 코드 미리보기
- SaveFileDialog를 활용한 이미지 저장
3. 기능 요구사항
필수 기능
1️⃣ QR 코드 생성:
- 사용자가 입력한 텍스트를 QR 코드로 생성
2️⃣ QR 코드 미리보기:
- 생성된 QR 코드를 PictureBox에 표시
3️⃣ QR 코드 저장:
- QR 코드를 이미지 파일로 저장
4. 실습: QR 코드 생성 애플리케이션 제작
1️⃣ 사전 준비
- ZXing.Net 라이브러리 설치:
- Visual Studio에서 NuGet 패키지 설치:
Install-Package ZXing.Net
- Visual Studio에서 NuGet 패키지 설치:
2️⃣ 폼 구성
- 폼(Form) 이름: Form1
- 컨트롤 배치:
컨트롤 타입 이름 위치 크기
TextBox | txtInput | 폼 상단 왼쪽 | (300 x 30) |
Button | btnGenerate | 폼 상단 오른쪽 | (100 x 30) |
PictureBox | picQRCode | 폼 중단 전체 | (300 x 300) |
Button | btnSave | 폼 하단 중앙 | (100 x 30) |
📌 폼 디자인 예시:
--------------------------------------------------
| [TextBox - QR 코드 텍스트 입력] [Generate 버튼] |
--------------------------------------------------
| [PictureBox - QR 코드 미리보기] |
--------------------------------------------------
| [Save 버튼] |
--------------------------------------------------
3️⃣ 코드 작성
(1) QR 코드 생성 및 미리보기
using System;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
namespace WindowsFormsApp_QRCode
{
public partial class Form1 : Form
{
private Bitmap _qrCodeImage;
public Form1()
{
InitializeComponent();
}
// QR 코드 생성
private void btnGenerate_Click(object sender, EventArgs e)
{
string inputText = txtInput.Text.Trim();
if (string.IsNullOrEmpty(inputText))
{
MessageBox.Show("QR 코드에 사용할 텍스트를 입력하세요.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
GenerateQRCode(inputText);
}
private void GenerateQRCode(string text)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = 300,
Height = 300,
Margin = 1
}
};
_qrCodeImage = writer.Write(text);
picQRCode.Image = _qrCodeImage;
}
}
}
(2) QR 코드 저장 기능
// QR 코드 이미지 저장
private void btnSave_Click(object sender, EventArgs e)
{
if (_qrCodeImage == null)
{
MessageBox.Show("저장할 QR 코드가 없습니다. 먼저 QR 코드를 생성하세요.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "PNG 파일 (*.png)|*.png|JPEG 파일 (*.jpg)|*.jpg";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
_qrCodeImage.Save(saveFileDialog.FileName);
MessageBox.Show("QR 코드가 성공적으로 저장되었습니다.", "완료", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
(3) Designer 코드
private void InitializeComponent()
{
this.txtInput = new TextBox();
this.btnGenerate = new Button();
this.picQRCode = new PictureBox();
this.btnSave = new Button();
// TextBox 설정
this.txtInput.Location = new System.Drawing.Point(10, 10);
this.txtInput.Size = new System.Drawing.Size(300, 30);
// Generate Button 설정
this.btnGenerate.Location = new System.Drawing.Point(320, 10);
this.btnGenerate.Size = new System.Drawing.Size(100, 30);
this.btnGenerate.Text = "Generate";
this.btnGenerate.Click += new EventHandler(this.btnGenerate_Click);
// PictureBox 설정
this.picQRCode.Location = new System.Drawing.Point(10, 50);
this.picQRCode.Size = new System.Drawing.Size(300, 300);
this.picQRCode.BorderStyle = BorderStyle.FixedSingle;
// Save Button 설정
this.btnSave.Location = new System.Drawing.Point(10, 360);
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.Text = "Save";
this.btnSave.Click += new EventHandler(this.btnSave_Click);
// Form 설정
this.ClientSize = new System.Drawing.Size(450, 400);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.btnGenerate);
this.Controls.Add(this.picQRCode);
this.Controls.Add(this.btnSave);
this.Text = "QR 코드 생성기";
}
3️⃣ 실행 결과
1️⃣ QR 코드 생성
- "Generate" 버튼 클릭 → TextBox에 입력한 텍스트를 기반으로 QR 코드 생성
2️⃣ QR 코드 미리보기
- PictureBox에 생성된 QR 코드 표시
3️⃣ QR 코드 저장
- "Save" 버튼 클릭 → SaveFileDialog로 파일 저장
5. 주요 개념 요약
- ZXing.Net 라이브러리: QR 코드 생성 및 바코드 처리를 위한 라이브러리
- BarcodeWriter: QR 코드 이미지를 생성하는 객체
- PictureBox: 생성된 QR 코드를 미리보기로 표시
- SaveFileDialog: QR 코드를 이미지 파일로 저장
📌 #CSharp #WindowsForms #QRCode #ZXingNet #QR코드생성 #PictureBox