Skip to content

Instantly share code, notes, and snippets.

@priesdelly
Last active February 24, 2019 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priesdelly/a735fca55b5892d2e7c164cd9ddb40a7 to your computer and use it in GitHub Desktop.
Save priesdelly/a735fca55b5892d2e7c164cd9ddb40a7 to your computer and use it in GitHub Desktop.
Example create barcode on .NET Framework
using Spire.Barcode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Barcode1 {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Event เมื่อเปิดโปรแกรมแล้วโหลดหน้า Form ขึ้นมา
//สร้าง Setting ของ Barcode ขึ้นมา
var settings = new BarcodeSettings() {
Data = "12345678901",
// ค่าตัวเลขของ Barcode ที่จะได้งาน ในที่นี้ผมใส่เป็นตัวเลขมั่วๆไปนะครับ
Type = BarCodeType.Code11,
// รูปแบบค่าตัวเลขของ Barcode ในที่นี้ใช้แบบ Code11
Unit = GraphicsUnit.Millimeter,
ShowTextOnBottom = true,
// แสดง Text ตัวเลข Barcode ไว้ด้านล่าง
TextFont = new Font("Arial", 10) // ชนิด Font ของตัวอักษรที่จะแสดงออกมา
};
// สร้าง Barocode Instance ขึ้นมาโดยอาศัย Setting ที่ได้สร้างขึ้นมา
var generator = new BarCodeGenerator(settings);
// สร้างเป็นรูปภาพขึ้นมาไปแสดงที่หน้าโปรแกรม
pictureBox1.Image = generator.GenerateImage();
}
private void btnSaveBarCodePicture_Click(object sender, EventArgs e)
{
// Event เมื่อทำการกดปุ่ม
// เปิดการใช้งาน SaveFileDialog
using(var sfd = new SaveFileDialog() {
Filter = "Image files (*.jpg)|*.jpg"
}) {
// สั่งให้ SaveFileDialog โชว์หน้า Dialog บน Class นี้ และรอผลลัพธ์ โดยใช้ If เช็คว่าถ้า OK ให้บันทึกรูป
if (sfd.ShowDialog(this) == DialogResult.OK) {
//ให้ PictureBox ทำการบันทึกรูปภาพลงในตำแหน่งที่ต้องการจาก SaveFileDialog และใน Format ที่ต้องการ
pictureBox1.Image.Save(sfd.FileName, ImageFormat.Jpeg);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment