SoFunction
Updated on 2025-03-04

Detailed explanation Generate QR code examples (using two methods)

Recently, I encountered the problem of generating QR codes when doing projects, and found that the most commonly used methods are used online. I visited the official website and read the example and wrote two demos. During the use, I found that both of them are very useful and have more functions. However, the dll file has 6 megabytes and only more than 400 K. Please choose according to your needs. The attached code is for reference only.

And it provides a demo written in VS2013 for free download. If you have any questions, please feel free to communicate.

private void CreateQrcode(string nr)

    {

      Bitmap bt;

      string enCodeString = nr;

      QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

      bt = (enCodeString, Encoding.UTF8);

      string filename = ("yyyymmddhhmmss");

      string path = ("~/image/") + filename + ".jpg";

      (path);

      (path);

      this. = "~/image/" + filename + ".jpg";

    }

:

protected void Button1_Click(object sender, EventArgs e)

    {

      using (var ms = new MemoryStream())

      {
        string stringtest = "China inghttp:///?&";

        GetQRCode(stringtest, ms);

         = "image/Png";

        ((), 0, (int));

        Image img = (ms);

        string filename = ("yyyymmddhhmmss");

        string path = ("~/image/") + filename + ".png";

        (path);

        ();

      } 
    }

    /// <summary>

    /// Get the QR code
    /// </summary>

    /// <param name="strContent">characts to be encoded</param>
    /// <param name="ms">Output Stream</param>
    ///&lt;returns&gt;True if the encoding succeeded, false if the content is empty or too large to fit in a QR code&lt;/returns&gt;

    public static bool GetQRCode(string strContent, MemoryStream ms)

    {

      ErrorCorrectionLevel Ecl = ; //Error correction level
      string Content = strContent;// Content to be encoded
      QuietZoneModules QuietZones = ; //Blank area
      int ModuleSize = 12;//size
      var encoder = new QrEncoder(Ecl);

      QrCode qr;

      if ((Content, out qr))//Encoding the content and saving the generated matrix
      {

        var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));

        (, , ms);

      }

      else

      {

        return false;

      }

      return true;

    }

Here is the download address:demo

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.