Build a QR Code Maker for Your Business
Welcome! This tutorial will show you how to create a Business QR Code Maker. We will use the C# programming language, and Visual Studio 2022 as our integrated development environment (IDE).
Begin by downloading VS2022 from Microsoft’s website:
https://visualstudio.microsoft.com/vs/
Install it, and then choose to create a new Windows Forms App using the C# programming language. Windows Forms, aka WinForms, is a graphical user interface library that allows us to build a large array of applications for Windows-based computers.
Press the “Next” button. Give the application a relevant name (I chose “Business QR Code Maker”) and then press “Next” again.
Choose the target framework. Keep the default .NET 6.0 option, which provides long-term support, and then press “Create”.
Visual Studio will now set up the environment for our application. This could take seconds to a few minutes, depending on your computer’s hardware, and then you will see the application form.
That’s the empty canvas where we will build the user interface elements. Drag and drop a Button, a PictureBox and a TextBox to our form, using the Toolbox on the left side of the screen. Arrange them as you want them, resize them, change the size of the form… let your inner designer run wild ;)
Here’s what I have gotten.
Click the button, and then change its text in the Solution Explorer, which is integrated into the sidebar on the right side of the screen.
You can change all sorts of properties for our Button, PictureBox and TextBox here: font type and size, text alignment, etc.
Creating a QR code maker from scratch would be a huge task, beyond the scope of this tutorial. Moreover, we don’t want to reinvent the wheel when we can use existing packages that can make our lives easier.
Right-click the project name in Solution Explorer, and then choose “Manage NuGet Packages…”.
A NuGet package consists of code donated by a generous developer. In this case, we will search for, and then install “QRCoder” by Raffael Herrmann.
Once that is done, we can close the NuGet tab and return to our application.
Double-click an empty area of the form to create an event handler that is fired when the form is loaded. Here’s what I have gotten so far.
Tell the application that we will be using QRCoder by adding its namespace definition at the top of the source file.
We also need to add a method that will pass the desired text (URL, etc.) to QRCoder.
Here’s what the edited source code looks like.
At this stage, our button doesn’t do anything useful; we need to add the code that makes it tick. That’s the way to do it.
It’s time to assign the event handler to our “Generate” button. So, click the button to select it, choose “Events” (the lightning bolt icon) in Solution Explorer, and then assign our button the “button1_Click” method.
Let's test our QR code maker! Choose Build -> Build Solution from the toolbar at the top of the screen, and then press the green triangle button to run the program.
If everything works as expected, you will see the application window.
Copy/paste your website’s URL in the text box at the top of the screen, and then press the “Generate” button. It works great. Can you believe it?
Actually, it doesn’t work that great, because the QR code is way too big in comparison with the size of the PictureBox.
Fortunately, the solution is simple: we need to tweak the numerical value of this line of code:
pictureBox1.Image = qrCode.GetGraphic(10);
In my case, the perfect value was 5, but you may want to tweak it until it does the job for you.
pictureBox1.Image = qrCode.GetGraphic(5);
Don’t forget to rebuild the solution; otherwise, you won’t see the effect of the change.
That’s a simple, and yet fully functional version of a QR code maker. The program needs more options: the ability to create various QR codes for text messages, locations, etc. It doesn’t save QR codes yet, and so on.
It only took me a few minutes to create it, though, so if you want me to build an advanced version of it, or any other business application you can think of, don’t hesitate to contact me.