SoFunction
Updated on 2025-03-07

C# After cropping the picture, use zxing to generate QR code example sharing

Copy the codeThe code is as follows:

/// <summary>
/// Generate QR code
/// </summary>
/// <param name="fileName">Generate QR code path</param>
/// <param name="url">Created content</param>
/// <param name="width">QR code width</param>
/// <param name="height">QR code high</param>
/// <param name="userFace">Logo image to be generated</param>
/// <returns></returns>
private Bitmap GetCodeImgUrl(string fileName, string url, int width, int height, string userFace)
{

    BarcodeWriter writer = new BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Renderer = new BitmapRenderer
        {
            Foreground =
        },
        Options = new
        {
            DisableECI = true,
            Height = height,
            Width = width,
            Margin = 0,
            CharacterSet = "UTF-8",
            ErrorCorrection =
        }
    };

    Bitmap bitmap = (url);
    if (!(userFace))
    {
        Bitmap bits = ()(userFace);
        if (bits != null)
        {                   
//Cut a 80*80 logo image
            ImageCut img = new ImageCut(0, 0, 80, 80);
            icon = (bits);
//userFace_b.jpg is a border image
            Bitmap bits2 = new (()( + "/user/userFace_b.jpg"), 84, 84);
            if (icon != null)
            {
                try
                { 
//Draw 2 borders, one is the logo, and the other adds a border around the logo
                    using (var graphics = (bitmap))
                    {
                        (bits2, ( - ) / 2, ( - ) / 2);
                        (icon, ( - ) / 2, ( - ) / 2);

                    }

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    ();
                    ();

                }
            }
            (fileName, );
        }

    }

    return bitmap;
}

Copy the codeThe code is as follows:

public class ImageCut
  {

      /// <summary>
///Cutting -- Use GDI+
      /// </summary>
/// <param name="b">original Bitmap</param>
/// <param name="StartX">Start coordinate X</param>
/// <param name="StartY">Start coordinate Y</param>
/// <param name="iWidth">Width</param>
/// <param name="iHeight">Height</param>
/// <returns>Cropped Bitmap</returns>
      public Bitmap KiCut(Bitmap b)
      {
          if (b == null)
          {
              return null;
          }
          int w = ;
          int h = ;
          int intWidth = 0;
          int intHeight = 0;
          if (h * Width / w > Height)
          {
              intWidth = Width;
              intHeight = h * Width / w;

          }
          else if (h * Width / w < Height)
          {
              intWidth = w * Height / h;
              intHeight = Height;

          }
          else
          {
              intWidth = Width;
              intHeight = Height;
          }

          Bitmap bmpOut_b = new (b, intWidth, intHeight);
          w = bmpOut_b.Width;
          h = bmpOut_b.Height;
        

          if (X >= w || Y >= h)
          {
              return null;
          }

          if (X + Width > w)
          {
              Width = w - X;
          }
          else
          {
              X = (w-Width) / 2;
          }

          if (Y + Height > h)
          {
              Height = h - Y;
          }
        

 

          try
          {
              Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);            
              Graphics g = (bmpOut);
              (bmpOut_b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), );
              ();

              return bmpOut;
          }
          catch
          {
              return null;
          }
      }

      public int X = 0;
      public int Y = 0;
      public int Width = 120;
      public int Height = 120;
      public ImageCut(int x, int y, int width, int heigth)
      {
          X = x;
          Y = y;
          Width = width;
          Height = heigth;
      }
  }

Copy the codeThe code is as follows:

  private void btnSubmit_Click(object sender, EventArgs e)
        {
            string UserId = "1245460396";   

            string curFilePath = "/user/";

            string curFileName_b = "DimensionalPig_" + UserId + "_b";
            string path = + curFilePath;
if ((path) == false)//Create a file folder if it does not exist
            {
                (path);
            }
string fileName_b = + curFilePath + "/" + curFileName_b + ".jpg";//Get uploaded file name

            string UserUrl = ("https:///u{0}", ());
            string userFace_b = + "/user/" + UserId + "_b.jpg";

            Bitmap bitmap_b = GetCodeImgUrl(fileName_b.Replace("_b.", "_b_ewm."), UserUrl, 400, 400, userFace_b);
            =()bitmap_b;
(fileName_b.Replace("_b.", "_b_ewm."));