How to Generate QR Code using C# Console Application?

The provided code demonstrates a simple console application that is capable of Generating the QR Code.

Step 1. Create a new C# console application project. In Visual Studio, you can follow these steps.

  • Open Visual Studio.
  • Select “Create a new project” or go to “File” > “New” > “Project.”
  • Choose “Console App (.NET)” as the project template.

 

 

Enter a name for your project (e.g., “QRCodeGenerate”) and Choose a location to save the project.

Select Framework and Click on Next.

  • Click “Create” to create the project.

Use the below code in Program.cs and replace all codes to Generate QR Codes.

using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using QRCoder;

namespace QRCodeGenerate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the data to encode in the QR code:");
            string data = Console.ReadLine();

            // Generate the QR code
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(20);


            // Specify the folder path to save the QR code image
            string folderPath = @"D:\QRCODE";

            // Create the folder if it doesn't exist
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            // Save the QR code as a PNG image file inside the specified folder
            string fileName = Path.Combine(folderPath, data + "_" + "QRCode.png");
            qrCodeImage.Save(fileName, ImageFormat.Png);

            // Display the QR code image using an image viewer application
            DisplayQRCodeImage(fileName);

            Console.ReadLine();
        }

        static void DisplayQRCodeImage(string imagePath)
        {
            try
            {
                // Check if the file exists
                if (System.IO.File.Exists(imagePath))
                {
                    // Use the default image viewer to open and display the QR code image
                    ProcessStartInfo psi = new ProcessStartInfo
                    {
                        FileName = imagePath,
                        UseShellExecute = true
                    };
                    Process.Start(psi);
                }
                else
                {
                    Console.WriteLine("QR code image not found.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Downloading and Installing NuGet Packages.

To test the code, follow these steps,

Step 1. Set Output Directory

Set the outputDirectory variable to the directory where you want to save the QR Code. In the provided code, it is set to @”D:\QRCODE”, but you can change it to any desired directory on your system.

Step 2. Enter the Data that you want to Generate the QR Code.

Enter any data you want to encode (e.g., a URL, some text, or contact information) and press Enter.

Step 3. Verify the QR in the Specific Location

The program will generate the QR code and save it as a PNG image in the folder D:\QRCODE (assuming this folder exists on your system).

After saving the image, the program will open the default image viewer to display the QR code image. You should be able to see the QR code image displayed by the image viewer.

If you encounter any issues or have further questions, feel free to let me know, and I’ll be glad to assist!

Thank you for reading, and I hope this post has helped provide you with a better understanding of How to Generate a QR Code using the QRCode Nuget Packet.

“Keep coding, keep innovating, and keep pushing the boundaries of what’s possible!

Happy Coding !!

Best and Most Recommended ASP.NET Core 8 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 8 web ranking. HostForLIFEASP.NET is highly recommended. In Europe, HostForLIFEASP.NET is the most popular option for first-time web hosts searching for an affordable plan.

Their standard price begins at only € 3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFEASP.NET guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFEASP.NET also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.