In this article I will explain with an example, how to generate QRCode with Logo (Image) in center in ASP.Net using C# and VB.Net.
 
 

Installing QRCoder package using Nuget

In order to install QRCoder library using Nuget, please refer my article Install QRCoder library using Nuget.
 
 

Logo Location

The Logo image is saved in the Folder (Directory) named images.
Generate QRCode with Logo in Center in ASP.Net
 
 

HTML Markup

The HTML Markup consist of following controls:
TextBox – For capturing user input.
Button – For generating QRCode.
The Button has been assigned with an OnClick event handler.
Image – For displaying QRCode.
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="OnGenerate" />
<br />
<asp:Image ID="imgQRCode" runat="server" Width="150" Height="150" Visible="false" />
 
 

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.
Note: The Logo Image is read from the Images folder within the Project directory (discussed earlier).
 
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 BASE64 string which is later assigned to the Image control.
C#
protected void OnGenerate(object sender, EventArgs e)
{
    string code = txtCode.Text;
 
    //Generate QRCode object.
    QRCodeGenerator qrGenerator = new QRCodeGenerator();
    QRCode qrCode = new QRCode(qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q));
 
    //Create a Bitmap using the QRCode.
    using (Bitmap bmpQRCode = qrCode.GetGraphic(30))
    {
        //Read the Logo Image.
        using (Bitmap bmpLogo = new Bitmap(Server.MapPath("~/images/ASPSnippets.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 using BASE64 string.
            imgQRCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
            imgQRCode.Visible = true;
        }
    }
}
 
VB.Net
Protected Sub OnGenerate(ByVal sender As Object, ByVal e As EventArgs)
    Dim code As String = txtCode.Text
 
    'Generate QRCode object.
    Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
    Dim qrCode As QRCode = New QRCode(qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q))
 
    'Create a Bitmap using the QRCode.
    Using bmpQRCode As Bitmap = qrCode.GetGraphic(30)
 
        'Read the Logo Image.
        Using bmpLogo As Bitmap = New Bitmap(Server.MapPath("~/images/ASPSnippets.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
 
        Using ms As MemoryStream = New MemoryStream()
            'Saving the QRCode Bitmap to MemoryStream.
            bmpQRCode.Save(ms, ImageFormat.Png)
 
            'Displaying the QRCode Image using BASE64 string.
            imgQRCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(ms.ToArray())
            imgQRCode.Visible = True
        End Using
    End Using
End Sub
 
 

Screenshot

Generate QRCode with Logo (Image) in Center in ASP.Net
 
 

Demo

 
 

Downloads