How to Rotate Barcode Image in C#

How to Rotate Barcode Image in C#

This step-by-step tutorial shows you how to rotate a barcode image in C#. You will learn to rotate the generated barcode at a specified angle, allowing for customization according to your needs in C# applications.

Benefits of Rotating Barcode Images

  1. Customization:
    • Adjust barcode orientation to fit varying application layouts.
  2. Improved Scanning:
    • Optimize the angle for better readability and scanning performance.
  3. Aesthetic Appeal:
    • Create visually appealing layouts by incorporating rotated barcodes.

Prerequisites: Preparing the Environment

  1. Set up Visual Studio or any compatible .NET IDE.
  2. Install the Aspose.BarCode library via the NuGet Package Manager.

Step-by-Step Guide to Rotate Barcode Image

Step 1: Install Aspose.BarCode

Add the Aspose.BarCode library to your project using NuGet.

Install-Package Aspose.BarCode

Step 2: Include Necessary Namespaces

Add references to the required Aspose.BarCode & Aspose.BarCode.Generation namespaces in your code.

using Aspose.BarCode;
using Aspose.BarCode.Generation;

Step 3: Create a BarcodeGenerator Object

Instantiate the BarcodeGenerator class using the desired encoding type, such as Code128.

BarcodeGenerator rotateBarCodeImage = new BarcodeGenerator(EncodeTypes.Code128);

Step 4: Specify the Barcode Text

Set the text you want to encode into the barcode using the CodeText property.

rotateBarCodeImage.CodeText = "Product Code 123";

Step 5: Set the Rotation Angle

Define the rotation angle for the barcode.

rotateBarCodeImage.Parameters.RotationAngle = 45; // Rotate 45 degrees

Step 6: Save the Rotated Barcode

Finally, save the rotated barcode image as a PNG file.

rotateBarCodeImage.Save("Rotated_BarCode_Image.png", BarCodeImageFormat.Png);

Complete Code Example to Rotate Barcode Image

Here’s the complete C# example demonstrating how to rotate a barcode image:

// Initiate barcode generator object with Code128 encode type
BarcodeGenerator rotateBarCodeImage = new BarcodeGenerator(EncodeTypes.Code128);
rotateBarCodeImage.CodeText = "Product Code 123";

// Set rotation of the barcode
rotateBarCodeImage.Parameters.RotationAngle = 45;

// Save rotated barcode image as PNG
rotateBarCodeImage.Save("Rotated_BarCode_Image.png", BarCodeImageFormat.Png);

Additional Information

  • Adjust the rotation angle according to your layout requirements.
  • You can also set additional properties like bar height and width for further customization.

Conclusion

This tutorial has shown you how to rotate barcode images in C# using Aspose.BarCode. The process is easy, requiring only a few lines of code to achieve the desired output. For further functionalities, explore tutorials on generating QR codes or manipulating other types of images.

 English