Nadesłany przez Tomasz Lubiński, 30 października 2019 21:39
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Code39Ext/Form1.cs:
//Kod kreskowy Extended Code 39 //(c) 2019 by Tomasz Lubinski //www.algorytm.org using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; namespace Barcodes.Code39Ext { public partial class Form1 : Form { private readonly char[] code39signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%' }; private readonly char[,] code39bars = { {'N', 'N', 'N', 'W', 'W', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W'}, {'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'W'}, {'W', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'W'}, {'W', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'W', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'W'}, {'W', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N'}, {'N', 'N', 'W', 'W', 'N', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W'}, {'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'W'}, {'W', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'W'}, {'W', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'W', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'W'}, {'W', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'W', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W'}, {'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'W'}, {'W', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'W'}, {'W', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'W', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'W'}, {'W', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'W', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'W', 'W', 'N'}, {'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W'}, {'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'W'}, {'W', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'W'}, {'W', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'W', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'W'}, {'W', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'W', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'W', 'N', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'N', 'N', 'W', 'N'}, {'N', 'W', 'N', 'N', 'N', 'W', 'N', 'W', 'N'}, {'N', 'N', 'N', 'W', 'N', 'W', 'N', 'W', 'N'} }; private readonly char[] start_stop = { 'N', 'W', 'N', 'N', 'W', 'N', 'W', 'N', 'N' }; private readonly string[] extendedCode39Replace = { "%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G", "$H", "$I", "$J", "$K", "$L", "$M", "$N", "$O", "$P", "$Q", "$R", "$S", "$T", "$U", "$V", "$W", "$X", "$Y", "$Z", "%A", "%B", "%C", "%D", "%E", " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G", "/H", "/I", "/J", "/K", "/L", "-", ".", "/O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "/Z", "%F", "%G", "%H", "%I", "%J", "%V", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "%K", "%L", "%M", "%N", "%O", "%W", "+A", "+B", "+C", "+D", "+E", "+F", "+G", "+H", "+I", "+J", "+K", "+L", "+M", "+N", "+O", "+P", "+Q", "+R", "+S", "+T", "+U", "+V", "+W", "+X", "+Y", "+Z", "%P", "%Q", "%R", "%S", "%T" }; /// <summary> /// Zainicjuj /// </summary> public Form1() { InitializeComponent(); pictureBox.Image = new Bitmap(pictureBox.Width, pictureBox.Height, PixelFormat.Format24bppRgb); System.Drawing.SolidBrush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); System.Drawing.Graphics graphics = Graphics.FromImage(pictureBox.Image); graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); pictureBox.Refresh(); whiteBrush.Dispose(); graphics.Dispose(); } /// <summary> /// Sprawdza czy podany txt mozna zakodowac Code 39 /// </summary> /// <param name="txt">tekst do zakodowania</param> /// <returns>true jezeli mozna zakodowac, false w przeciwnym razie</returns> public bool check(string txt) { for (int i = 0; i < txt.Length; i++) { if (checkLetter(txt[i]) == false) return false; } return true; } /// <summary> /// Sprawdza czy podany znak mozna zakodowac Code 39 /// </summary> /// <param name="ch">znak do zakodowania</param> /// <returns>true jezeli mozna zakodowac, false w przeciwnym razie</returns> private bool checkLetter(char ch) { for (int i = 0; i < code39signs.Length; i++) { if (code39signs[i] == ch) return true; } return false; } /// <summary> /// Zwraca wartosc danego znaku /// </summary> /// <param name="ch">znak</param> /// <returns>wartosc danego znaku</returns> private int getLetterValue(char ch) { for (int i = 0; i < code39signs.Length; i++) if (code39signs[i] == ch) return i; return -1; } /// <summary> /// Konwertuje string do zbioru Code 39 /// </summary> /// <param name="?"></param> /// <returns></returns> private string convertToCode39(string code39ext) { string result = ""; for (int i=0; i<code39ext.Length; i++) { if ((code39ext[i] >= 0) && (code39ext[i] <= 127)) result += extendedCode39Replace[code39ext[i]]; else result += code39ext[i]; } return result; } /// <summary> /// Generuj Extended Code 39 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Generate_Click(object sender, EventArgs e) { //Zainicjuj narzedzia do rysowania System.Drawing.SolidBrush whiteBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White); System.Drawing.SolidBrush blackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Graphics graphics = Graphics.FromImage(pictureBox.Image); Font drawFont = new Font("Arial", 10); //Wyczysc obszar rysowania graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, pictureBox.Width, pictureBox.Height)); //Przekonwertuj znaki na Code 39 i sprawdz czy podany ciag mozna zakodowac string code = convertToCode39(Code.Text); if (check(code) == false) { MessageBox.Show("Nieprawidłowe znaki!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Wygeneruj i narysuj kod char[] bars = generateBars(code); int pos = 10; int width = 0; for (int i = 0; i < bars.Length; i++) { //Sprawdz czy teraz bedzie pasek szeroki czy waski if (bars[i] == 'N') { width = 2; } else { width = 4; } //Sprawdz czy pasek bialy czy czarny (rysujemy tylko czarne, bo tlo jest biale) if ((i % 2) == 0) { graphics.FillRectangle(blackBrush, pos, 10, width, 90); } pos += width; } //Wypisz kodowany ciag StringFormat center = new StringFormat(); center.Alignment = StringAlignment.Center; graphics.DrawString(Code.Text, drawFont, blackBrush, pos / 2, 102, center); //Odswiez pictureBox.Refresh(); //Zwolinij zasoby whiteBrush.Dispose(); blackBrush.Dispose(); graphics.Dispose(); } /// <summary> /// Generuje kod kreskowy /// </summary> /// <param name="code">string do zakodowania kodem kreskowym</param> /// <returns>kod kreskowy - W pasek szeroki, N - pasek waski</returns> private char[] generateBars(string code) { char[] bars = new char[10 * (code.Length+2)]; //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[j] = start_stop[j]; } bars[9] = 'N'; //Dodaj odstep //Dodaj kolejne znaki for (int i = 0; i < code.Length; i++) { int letterValue = getLetterValue(code[i]); for (int j = 0; j < 9; j++) { bars[i * 10 + 10 + j] = code39bars[letterValue, j]; } bars[i * 10 + 19] = 'N'; //Dodaj odstep } //Dodaj znak start/stop for (int j = 0; j < 9; j++) { bars[code.Length * 10 + 10 + j] = start_stop[j]; } bars[code.Length * 10 + 10 + 9] = 'N'; //Dodaj odstep return bars; } } }