In this article I will explain with an example, how to generate
QRCode with Logo (Image) in center in C# and VB.Net.
Installing QRCoder package using Nuget
Form Design
The following Form consists of:
TextBox - For capturing user input.
Button – For generating
QRCode.
PictureBox - For displaying the
QRCode image.
Namespaces
You will need to import the following namespaces.
C#
using QRCoder;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports QRCoder
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Generating QRCode with Logo in Center using C# and VB.Net
When the Generate
Button is clicked, the
QRCode object is generated using the text provided by the User with the help of
QRCodeGenerator and
QRCode class.
Then, two Bitmaps are created, one for the
QRCode and another for the Logo Image.
After setting the resolution, the Coordinates for drawing the Logo Image in the center of the
QRCode are calculated and the Logo Image is drawn in the center of the
QRCode Bitmap.
Finally, the Bitmap is saved to the MemoryStream and converted into bytes and assigned to the Image control.
C#
private void OnGenerate(object sender, EventArgs e)
{
//Generate QRCode object.
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCode qrCode = new QRCode(qrGenerator.CreateQrCode(txtCode.Text, QRCodeGenerator.ECCLevel.Q));
//Create a Bitmap using the QRCode.
using (Bitmap bmpQRCode = qrCode.GetGraphic(10))
{
//Read the Logo Image.
using (Bitmap bmpLogo = new Bitmap("E:\\Images\\Logo.png"))
{
//Set the Resolution.
bmpLogo.SetResolution(80f, 80f);
//Calculating the coordinates for the Logo Image.
int x = (bmpQRCode.Height -bmpLogo.Height) / 2;
int y = (bmpQRCode.Width -bmpLogo.Width) / 2;
Point p = new Point(x, y);
//Drawing the Logo Image in the center of the QRCode.
Graphics graphics = Graphics.FromImage(bmpQRCode);
graphics.DrawImage(bmpLogo, p);
}
using (MemoryStream ms = new MemoryStream())
{
//Saving the QRCode Bitmap to MemoryStream.
bmpQRCode.Save(ms, ImageFormat.Png);
//Displaying the QRCode Image.
pbQRCode.Image = Image.FromStream(new MemoryStream(ms.ToArray()));
pbQRCode.SizeMode = PictureBoxSizeMode.Zoom;
pbQRCode.Visible = true;
}
}
}
VB.Net
Private Sub OnGenerate(sender As Object, e As EventArgs) Handles btnGenerate.Click
'Generate QRCode object.
Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
Dim qrCode As QRCode = New QRCode(qrGenerator.CreateQrCode(txtCode.Text, QRCodeGenerator.ECCLevel.Q))
'Create a Bitmap using the QRCode.
Using bmpQRCode As Bitmap = qrCode.GetGraphic(10)
'Read the Logo Image.
Using bmpLogo As Bitmap = New Bitmap("E:\Images\Logo.png")
'Set the Resolution.
bmpLogo.SetResolution(80.0F, 80.0F)
'Calculating the coordinates for the Logo Image.
Dim x As Integer = (bmpQRCode.Height - bmpLogo.Height) / 2
Dim y As Integer = (bmpQRCode.Width - bmpLogo.Width) / 2
Dim p As Point = New Point(x, y)
'Drawing the Logo Image in the center of the QRCode.
Dim graphics As Graphics = Graphics.FromImage(bmpQRCode)
graphics.DrawImage(bmpLogo, p)
End Using
'Saving the QRCode Bitmap to MemoryStream.
Using ms As MemoryStream = New MemoryStream()
bmpQRCode.Save(ms,ImageFormat.Png)
'Displaying the QRCode Image.
pbQRCode.Image = Image.FromStream(New MemoryStream(ms.ToArray()))
pbQRCode.SizeMode = PictureBoxSizeMode.Zoom
pbQRCode.Visible = True
End Using
End Using
End Sub
Screenshot
Downloads