SoFunction
Updated on 2025-03-06

Example of C# dynamically generates custom QR code pictures based on QRCode

This article describes the C# dynamically generates custom QR code images based on QRCode. Share it for your reference, as follows:

The QR code has been spread all over the country for a long time. I always thought it was a magical thing. In fact, after careful research, I found that it was not as mysterious as I thought. It happened that the QR code needed to be dynamically generated in the project recently. After solving the actual problems, I would briefly summarize and organize them. In addition to dynamically generating QR codes, the project also implements dynamically generating custom pictures, and the QR code can be an element in it.

Set the data source of the image to dynamic image

<body>
  <form  runat="server" >
  <div>
    <img src="?type=2" />
  </div>
  </form>
</body>

Dynamically generate pictures

File content

protected void Page_Load(object sender, EventArgs e)
{
  string type = ["type"].ToString();
  Bitmap codeImage = Create_QRCode("Share to get more,I'll do my best(5201314)", 6);
  MemoryStream ms = Create_ImgCode(codeImage, "Share to get more, I do my best", "5201314", type);
  ();
   = "image/Png";
  (());
  ();
}
private Bitmap Create_QRCode(string codeNumber, int size)
{
  //Create a QR code generation class  QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
  //Set the encoding mode   = QRCodeEncoder.ENCODE_MODE.BYTE;
  //Set the encoding measurement   = size;
  //Set the encoding version   = 10;
  //Set encoding error correction   = QRCodeEncoder.ERROR_CORRECTION.M;
  //Generate QR code pictures   codeImage = (codeNumber, Encoding.UTF8);
  return codeImage;
}
/// &lt;summary&gt;
/// Generate custom pictures/// &lt;/summary&gt;
/// <param name="codeImage">generated QR code</param>/// <param name="objectName">Object name</param>/// <returns>Custom picture memory stream</returns>private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type)
{
  string path = ;
  if (type == "1")
  {
    //Set background pictures    path = ("Images/");
  }
  else if (type == "2")
  {
    //Set background pictures    path = ("Images/");
  }
   img = (path);
  Bitmap bg = new Bitmap(img);
  //Create a brush for canvas bg (picture bg)  Graphics g = (bg);
  if (type == "1")
  {
    //【1】Draw the bitmap file codeImage on canvas g    //【2】The upper left corner of codeImage is 25px from the left boundary of the canvas and 56px from the upper boundary of the canvas    //【3】The length of codeImage is the original length and width is the original width    (codeImage, 25, 56, , );
  }
  else if (type == "2")
  {
    (codeImage, 132, 19, 162, 162);
     b = new SolidBrush();
    Font font = new Font("Song-style", 8, );
    StringFormat sf = new StringFormat();
     = ; // Vertical center     = ;    // Horizontal left alignment    //String is also drawn on the canvas. When the length of the drawn string is greater than 112px, it will automatically wrap the lines when the drawn string length is greater than 112px.    SizeF stringSize = ("My declaration:", font, 112, sf);
    int nWidth = (int) + 1;
    int nHeight = (int) + 1;
    RectangleF rf = new Rectangle(new Point(12, 64), new Size(nWidth, nHeight));
    ("My declaration:", font, b, rf, sf);
    stringSize = (objectName, font, 112, sf);
    int objectWidth = (int) + 1;
    int objectHeight = (int) + 1;
    rf = new Rectangle(new Point(12, 64 + nHeight + 8), new Size(objectWidth, objectHeight));
    (objectName, font, b, rf, sf);
    SizeF stringSize1 = ("Lucky Number:", font, 112, sf);
    nWidth = (int) + 1;
    nHeight = (int) + 1;
    RectangleF rf1 = new Rectangle(new Point(12, 136), new Size(nWidth, nHeight));
    ("Lucky Number:", font, b, rf1, sf);
    stringSize1 = (objectCode, font, 112, sf);
    objectWidth = (int) + 1;
    objectHeight = (int) + 1;
    rf1 = new Rectangle(new Point(12, 136 + nHeight + 8), new Size(objectWidth, objectHeight));
    (objectCode, font, b, rf1, sf);
  }
  ();
  ();
   ms = new ();
  (ms, );
  //Save canvas bg (image bg) to the specified path  path = ("Images");
  (path + "\\", );
  ();
  ();
  return ms;
}

Click hereDownload this site

PS: This site also provides an online QR code generation tool with very powerful functions, which can realize the QR code generation and logo icon addition functions of text, phone numbers, text messages, emails, websites, etc.:

Online QR code generation tool (enhanced version):
http://tools./transcoding/jb51qrcode

For more information about C# related content, please check out the topic of this site:Summary of C# picture operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.