How to Create and Read Barcodes in C#
Tags: dotnet, createbarcode, readbarcode, restapi
Published: 2019-10-25 10:04:45

If I have a requirement to create and read barcodes in my application, I would prefer to use some webservice. Because it eliminates the overhead to install and maintain a separate software to create and read barcodes. In this article I will share a REST API solution, Aspose.BarCode Cloud, for working with the barcode in your application.

[Aspose.BarCode Cloud](https://products.aspose.cloud/barcode) is a barcode generation and recognition REST API solution, that supports the generation and recognition of 1D (Linear), 2D and postal barcodes of 60+ symbologies.

In this article, I will use Aspose.BarCode Cloud SDK for .NET with C#. However, if you are using some other programming language you can check [SDKs](https://github.com/aspose-barcode-cloud) of other popular programming languages and can user it directly in your favorite platform. You can read the [blog post](https://blog.aspose.cloud/2019/10/24/how-to-create-and-read-barcodes-with-c-net-rest-api/) for more details.

Let's start the code:

# Create Barcode
**Step 1:** First thing first, [sign up](https://docs.aspose.cloud/display/totalcloud/Creating+and+Managing+Account) with aspose.cloud to get your AppSID and AppKey
**Step 2:** Create a new project in Visual Studio and install Aspose.BarCode SDK for .NET package from NuGet
```PM> Install-Package Aspose.BarCode-Cloud```
**Step 3:** Copy paste the following code in the new project to create a barcode and run it. We are creating datamatrix 2D barcode as PNG image. The used API method returns barcode image as stream, you can consume it as per your requirement.

```csharp
// Get App Key and App SID from https://dashboard.aspose.cloud/
// Instantiate Aspose BarCode Cloud API SDK
var barCodeApi = new BarCodeApi(AppKey, AppSid);

// Set Filename of image
String datafolder = "C:/Temp/";

// Set Filename of image
String name = "sample-barcode";

// Set Text to encode inside barcode
String text = "Aspose.BarCode";

// Set Barcode Symbology
String type = "datamatrix";

// Set Barcode Image Format
String format = "PNG";

// Sets if checksum will be added to barcode image.
String enableChecksum = null;

// Set optional params (if any) 
float? resolutionX = null;
float? resolutionY = null;
float? dimensionX = null;
float? dimensionY = null;
try
    {
     // Invoke Aspose.BarCode Cloud SDK API to create barcode and returns result as stream in the response
     using (Stream response = barCodeApi.BarCodeGetBarCodeGenerate(new BarCodeGetBarCodeGenerateRequest(text, type, format, resolutionX, resolutionY, dimensionX, dimensionY, enableChecksum)))
     // Download generated barcode from api response
     using (FileStream stream = File.Create(datafolder + name + "." + format))
     {
         response.CopyTo(stream);
     }
     Console.WriteLine("Generate Barcode to Local File, Done!");
}
catch (Exception ex)
{
     Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
```


# Read Barcode

To read barcode, repeat the above steps 1 and 2, if required.

**Step 3:** Copy paste the following code in the new project to read the barcode image from local drive. It recognizes the barcode image and return the barcode type and value.
```csharp
// Get App Key and App SID from https://dashboard.aspose.cloud/
// Instantiate Aspose BarCode Cloud API SDK
var barCodeApi = new BarCodeApi(AppKey, AppSid);

// Set Filename of image
String datafolder = "C:/Temp/";
            
// Set input file name
String name = "sample-barcode.png";

// The barcode type. If this parameter is empty, autodetection of all supported types is used.
String type = "";

// Set mode for checksum validation during recognition
String checksumValidation = "";

// Set if FNC symbol stripping should be performed
bool stripFnc = false;

// Set recognition of rotated barcode
int rotationAngle = 0;

// Set the image file url 
String url = null;

Stream file = System.IO.File.OpenRead(datafolder + name);

try
  {
     // Invoke Aspose.BarCode Cloud SDK API to read barcode from local file
     var apiResponse = barCodeApi.BarCodePostBarCodeRecognizeFromUrlorContent( new BarCodePostBarCodeRecognizeFromUrlorContentRequest(type, checksumValidation, stripFnc, rotationAngle, url, file));

     if (apiResponse != null)
      {
        foreach (var barcode in apiResponse.BarCodes)
        {
         Console.WriteLine("Codetext: " + barcode.BarCodeValue + "\nType: " + barcode.BarCodeType);
        }
         Console.WriteLine("Read Barcode from Local File, Done!");
     }
}
catch (Exception ex)
{
     Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
```