QR Code in B2B signed Invoices

This code will save the QR Code Image to a specified path if provided otherwise on Application Running Path. Once QR is Saved, you can load the image in Crysal Report at run time.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing; // Make sure to install ZXing.Net via NuGet

public class QRCodeGenerator
{
    /// <summary>
    /// Generates a QR Code from the provided content and saves it as an image file.
    /// Developed by Muhammad Ather Shahzad.
    /// </summary>
    /// <param name="content">The content to encode into the QR code.</param>
    /// <param name="width">The width of the generated QR code (default: 500).</param>
    /// <param name="height">The height of the generated QR code (default: 500).</param>
    /// <param name="savePath">The file path where the QR code image will be saved. If null, the image will be saved in the application's default path.</param>
    public static void GenerateQRCode(string content, int width = 500, int height = 500, string savePath = null)
    {
        // Create a barcode writer for QR Code generation
        var writer = new BarcodeWriterPixelData
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Width = width,
                Height = height,
                Margin = 0
            }
        };

        // Write QR code data
        var pixelData = writer.Write(content);

        // Create a Bitmap image to hold the QR code
        using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
        {
            // Copy pixel data into the Bitmap
            using (var ms = new MemoryStream())
            {
                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), 
                                                 ImageLockMode.WriteOnly, 
                                                 PixelFormat.Format32bppRgb);
                try
                {
                    // Copy the QR code pixel data into the Bitmap
                    System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                }
                finally
                {
                    // Unlock the Bitmap data
                    bitmap.UnlockBits(bitmapData);
                }

                // If a save path is specified, save the image to that location
                if (!string.IsNullOrEmpty(savePath))
                {
                    string directory = Path.GetDirectoryName(savePath);
                    if (!Directory.Exists(directory))
                    {
                        // Create directory if it doesn't exist
                        Directory.CreateDirectory(directory);
                    }
                    // Save the QR code as a JPEG image
                    bitmap.Save(savePath, ImageFormat.Jpeg);
                }
                else
                {
                    // If no save path is provided, save it in the application's default path
                    string defaultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "QRCode.jpg");
                    bitmap.Save(defaultPath, ImageFormat.Jpeg);
                }
            }
        }
    }
}