Przejdź do treści stopki
KORZYSTANIE Z IRONBARCODE

Jak utworzyć generator kodu kreskowego Code 39 w C#

Barcodes have become an integral part of modern business operations, facilitating efficient inventory management, point-of-sale transactions, and data tracking. Among the various barcode symbologies, Code 39 stands out as one of the most widely used and versatile options.

A checksum digit, also known as a check digit or validation digit, is a digit added to a sequence of numbers (or alphanumeric characters) to help detect errors in the data. The purpose of the checksum digit is to ensure the integrity of the data by providing a simple method of error detection during data transmission or processing. One common application of a checksum digit is in barcodes, where it is often used to verify the accuracy of the scanned data. One of the barcode symbologies that uses a checksum digit is Code 39.

Code 39 encodes alphanumeric characters, including uppercase letters, numeric digits, and a few special characters. It includes a start character, an optional checksum character, and a stop character, making it self-checking to ensure accurate data capture. Also, human-readable text can be displayed below the generated barcode image.

IronBarcode (produced by Iron Software) is the leading .NET C# Barcode library for reading and creating barcodes. The User-friendly API allows developers to add barcode functionality to .NET applications in minutes. Developers can generate a Code 39 barcoding project and barcoding test in minutes using this library.

In this article, we'll explore the process of building a Code 39 barcode generator using IronBarcode.

How to Create a Code 39 Barcode Generator in C

  1. Create a new C# project in Visual Studio
  2. Install the IronBarcode library and add it to your project.
  3. Generate Code 39 Barcode using the IronBarcode class library
  4. Add annotation text to the Code 39 Barcode images
  5. Add styling to the Code 39 Barcode images

Wymagania wstępne

  1. Visual Studio: Ensure you have Visual Studio or any other C# development environment installed.
  2. NuGet Package Manager: Make sure you can use NuGet to manage packages in your project.

Step 1: Create a New C# Project in Visual Studio

Create a new C# console application or use an existing project where you want to generate new barcode images. This library can also be used on a .NET Windows forms application. For the sake of this tutorial, let's consider a console application.

Select the console application template and click next.

How to Create a Code 39 Barcode Generator in C#: Figure 1 - Selecting the console application template

In the next step, you can provide the solution and project names.

How to Create a Code 39 Barcode Generator in C#: Figure 2 - Configuring the project name and solution

Select the .NET Version and click "Create".

How to Create a Code 39 Barcode Generator in C#: Figure 3 - Creating the project with the correct .NET version

Step 2: Install the IronBarcode library

IronBarcode can be installed from the NuGet Package Manager.

How to Create a Code 39 Barcode Generator in C#: Figure 4 - IronBarcode on the NuGet package manager

It can also be installed from the Visual Studio package manager. Search for IronBarcode in Package Manager and click install.

How to Create a Code 39 Barcode Generator in C#: Figure 5 - Installing IronBarcode from the Visual Studio Package manager

Step 3: Generate Code 39 Barcode using the IronBarcode Library

Now, let's write the code to generate Code 39 barcodes using the IronBarcode library. Below is a simple example:

using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode using the BarcodeWriter class
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png");
using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode using the BarcodeWriter class
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png");
Imports IronBarCode

Console.WriteLine("Code 39 Barcode Generator")

' Generate a Code 39 Barcode using the BarcodeWriter class
Dim code39Barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code39)

' Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png")
$vbLabelText   $csharpLabel

This simple program initializes a BarcodeWriter C# class, sets the encoding format to Code39, and generates a barcode PNG using the provided data. The barcode image is then saved as ironSoftwareBarcode.png.

Wynik:

How to Create a Code 39 Barcode Generator in C#: Figure 6 - Outputted barcode image encoded in Code 39

Here we are using the BarcodeWriter class from the IronBarcode library to create Code 39 barcodes with the URL data provided. Every time the code is run, a new barcode image is generated.

Add Annotation Text to Code 39 Barcode images

Annotation text can be added to barcodes easily with IronBarcode. The BarcodeWriter object generates a barcode object which has a Fluent API, allowing barcode text to be set in a single line of code.

using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:");
code39Barcode.AddBarcodeValueTextBelowBarcode();

// Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png");
using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:");
code39Barcode.AddBarcodeValueTextBelowBarcode();

// Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png");
Imports IronBarCode

Console.WriteLine("Code 39 Barcode Generator")

' Generate a Code 39 Barcode
Dim code39Barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code39)

' Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:")
code39Barcode.AddBarcodeValueTextBelowBarcode()

' Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png")
$vbLabelText   $csharpLabel

Wynik:

How to Create a Code 39 Barcode Generator in C#: Figure 7 - Outputted annotated barcode image from the previous code

Here you can see the product URL human-readable text is added above the barcode, and the value of the barcode in human-readable text is added below the barcode image.

Add Styling to Code 39 Barcode images

IronBarcode allows the barcode and the human-readable text to be styled. Usually, styling barcodes include resizing, setting margins, changing background colors, changing barcode colors, fonts, and verifying that the output barcode is still readable. All these methods are available on the BarcodeWriter object. The width and height are set in pixels.

How to Create a Code 39 Barcode Generator in C#: Figure 8 - How to simply add styling to a BarcodeWriter object

Generate with a Stream Object

The BarcodeWriter object can also work with a Stream object as shown below. This is particularly helpful in web API applications to save memory. A graphics object can also take advantage of this.

How to Create a Code 39 Barcode Generator in C#: Figure 9 - Generating a barcode with a Stream type object

Licensing (Free Trial Available)

IronBarcode requires a license key. The key needs to be placed in appsettings.json.

{
    "IronBarCode.LicenseKey": "MYLICENSE.KEY.TRIAL"
}

Provide your email to get a trial license. After submitting your email ID, the key will be delivered via email.

How to Create a Code 39 Barcode Generator in C#: Figure 10 - Popup showing a successful trial license application

Wnioski

In this comprehensive guide, we have explored the process of building a Code 39 barcode generator using the C# programming language. Kod 39 jest wszechstronny i szeroko stosowany w symbolice BarCode, znany ze swojej prostoty i możliwości kodowania znaków alfanumerycznych. Wykorzystując możliwości biblioteki IronBarcode, pokazaliśmy krok po kroku, jak stworzyć aplikację w języku C#, która generuje kody kreskowe Code 39 z opcjonalnymi cyframi kontrolnymi.

Wraz z postępem technologicznym coraz większego znaczenia nabiera dokładne i wydajne kodowanie oraz dekodowanie danych. Stworzenie generatora BarCode kodów kreskowych Code 39 w języku C# nie tylko zapewnia praktyczne narzędzie dla firm i programistów, ale służy również jako ćwiczenie edukacyjne w zakresie zrozumieniuiuiuiuia symboli BarCode, algorytmów sum kontrolnych oraz integracji bibliotek stron trzecich w aplikacjach C#.

Podsumowując, niniejszy przewodnik wyposaża programistów w wiedzę i narzędzia niezbędne do stworzenia solidnego generatora kodów kreskowych Code 39, sprzyjając integracji niezawodnych rozwiązań w zakresie kodów kreskowych z ich projektami. Niezależnie od tego, czy jesteś doświadczonym programistą, czy dopiero zaczynasz przygodę z generowaniem kodów kreskowych, ten artykuł stanowi solidną podstawę do dalszego zgłębiania tematu i dostosowywania rozwiązań do konkretnych wymagań Twoich aplikacji.

Często Zadawane Pytania

Czym jest Code 39 i dlaczego jest popularny?

Code 39 to symbolika BARCODE-ów, która koduje znaki alfanumeryczne, w tym wielkie litery, cyfry i kilka znaków specjalnych. Jest popularna ze względu na swoją prostotę i wszechstronność, dzięki czemu nadaje się do różnych zastosowań w działalności biznesowej.

Jak stworzyć generator BARCODE Code 39 w języku C#?

Możesz stworzyć generator kodów kreskowych Code 39 w języku C#, korzystając z biblioteki IronBarcode. Najpierw skonfiguruj projekt C# w Visual Studio, a następnie zainstaluj bibliotekę IronBarcode za pomocą menedżera pakietów NuGet. Użyj klasy BarcodeWriter do generowania i zapisywania obrazów kodów kreskowych.

Jakie kroki należy wykonać, aby zainstalować IronBarcode w projekcie C#?

Aby zainstalować IronBarcode w projekcie C#, otwórz menedżera pakietów NuGet w Visual Studio, wyszukaj „IronBarcode” i kliknij „zainstaluj”. Spowoduje to dodanie niezbędnej biblioteki IronBarcode do projektu, umożliwiając generowanie kodów kreskowych.

Jak mogę zwiększyć widoczność BarCode'ów Code 39 w mojej aplikacji?

IronBarcode pozwala ulepszyć kody kreskowe Code 39 poprzez dodanie tekstu adnotacji za pomocą metod takich jak AddAnnotationTextAboveBarcode i AddBarcodeValueTextBelowBarcode, a także poprzez dostosowanie kolorów, czcionek i rozmiarów.

Czy mogę efektywnie generować BARCODES Code 39 w aplikacji internetowej?

Tak, korzystając z IronBarcode wraz z obiektem Stream, można efektywnie generować kody kreskowe Code 39 w aplikacjach internetowych, oszczędzając pamięć i płynnie integrując się z obiektami graficznymi.

Jakie opcje licencyjne są dostępne dla korzystania z IronBarcode?

IronBarcode wymaga licencji, aby korzystać z pełnej funkcjonalności. Programiści mogą uzyskać Licencję Trial, podając swój adres e-mail, a następnie otrzymując klucz licencyjny pocztą elektroniczną do celów ewaluacyjnych.

W jaki sposób suma kontrolna zwiększa integralność kodu kreskowego BarCode?

Suma kontrolna to opcjonalna funkcja w BARCODES-ach, takich jak Code 39, która weryfikuje dokładność danych poprzez wykrywanie błędów podczas transmisji lub przetwarzania, zapewniając integralność zeskanowanych danych.

Jakie są zalety korzystania z IronBarcode for .NET?

IronBarcode umożliwia szybką integrację funkcji kodów kreskowych z aplikacjami .NET, obsługując różne opcje dostosowywania i zapewniając efektywne wykorzystanie pamięci, co czyni go cennym narzędziem dla programistów.

Jordi Bardia
Inżynier oprogramowania
Jordi jest najbardziej biegły w Pythonie, C# i C++. Kiedy nie wykorzystuje swoich umiejętności w Iron Software, programuje gry. Dzieląc odpowiedzialność za testowanie produktów, rozwój produktów i badania, Jordi wnosi ogromną wartość do ciągłej poprawy produktów. Różnorodne doświadczenia ...
Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie