locked
Convert any doc file to QR-Code RRS feed

  • Question

  • User1997423929 posted
    How I can convert any type of doc file to QR-Code in c#?
    Friday, December 14, 2018 3:51 PM

Answers

  • User-893317190 posted

    Hi Adwin Jha,

    You could use QRCoder to generate QR-Code in c#,please use nuget,  input QRCoder  and  install it into your project.

    Below is sample code.

       string strCode = File.ReadAllText(Server.MapPath("/csharpDemo2/qrcode.txt"));//write your path of the text
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);
    
                // qrcode.GetGraphic 
                Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
                qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg"));//write your path where you want to store the qr-code image.


    // if you want to write to browser as binary, you could try the code below.
    // remove qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg")) and write
    MemoryStream ms = new MemoryStream();
    qrCodeImage.Save(ms,ImageFormat.Jpeg);

    HttpContext.Current.Response.ContentType="image/Jpeg"
    HttpContext.Current.Response.BinaryWrite(ms.ToArray());

    Below is the explanation for the method qrcode.GetGraphic.

    /* GetGraphic method parameter description
                     Public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true)
                 *
                     Int pixelsPerModule: The pixel size of the generated QR image
                 *
                     Color darkColor: Dark color Normally set to Color.Black Black
                 *
                     Color lightColor: Bright color is generally set to Color.White white
                 *
                     Bitmap icon : QR code Watermark icon For example: Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); The default is NULL, plus an icon will be displayed in the middle of the QR code.
                 *
                     Int iconSizePercent: The size ratio of the watermark icon, which can be set according to your preferences.
                 *
                     Int iconBorderWidth: The border of the watermark icon
                 *
                     Bool drawQuietZones: A still area, located on a blank border on one side of the QR code, used to prevent the reader from obtaining information that is unrelated to the QR code being viewed. That is, whether to draw a blank border area of ​​the QR code.
    Default is true.    */

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 3:09 AM

All replies

  • User379720387 posted

    The data density of a QR tag is not that great.

    A business card worth of data is pretty much it.

    But you can use it to trigger an action and link through to somewhere else.

    https://code.msdn.microsoft.com/ZXINGNET-QRCode-Generator-05128cfb

    Saturday, December 15, 2018 2:43 PM
  • User-893317190 posted

    Hi Adwin Jha,

    You could use QRCoder to generate QR-Code in c#,please use nuget,  input QRCoder  and  install it into your project.

    Below is sample code.

       string strCode = File.ReadAllText(Server.MapPath("/csharpDemo2/qrcode.txt"));//write your path of the text
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);
    
                // qrcode.GetGraphic 
                Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
                qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg"));//write your path where you want to store the qr-code image.


    // if you want to write to browser as binary, you could try the code below.
    // remove qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg")) and write
    MemoryStream ms = new MemoryStream();
    qrCodeImage.Save(ms,ImageFormat.Jpeg);

    HttpContext.Current.Response.ContentType="image/Jpeg"
    HttpContext.Current.Response.BinaryWrite(ms.ToArray());

    Below is the explanation for the method qrcode.GetGraphic.

    /* GetGraphic method parameter description
                     Public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true)
                 *
                     Int pixelsPerModule: The pixel size of the generated QR image
                 *
                     Color darkColor: Dark color Normally set to Color.Black Black
                 *
                     Color lightColor: Bright color is generally set to Color.White white
                 *
                     Bitmap icon : QR code Watermark icon For example: Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); The default is NULL, plus an icon will be displayed in the middle of the QR code.
                 *
                     Int iconSizePercent: The size ratio of the watermark icon, which can be set according to your preferences.
                 *
                     Int iconBorderWidth: The border of the watermark icon
                 *
                     Bool drawQuietZones: A still area, located on a blank border on one side of the QR code, used to prevent the reader from obtaining information that is unrelated to the QR code being viewed. That is, whether to draw a blank border area of ​​the QR code.
    Default is true.    */

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 3:09 AM
  • User1997423929 posted

    In this way I can only convert the text file but I am not able to convert the word or image file.

    Monday, December 17, 2018 11:56 AM
  • User-893317190 posted

    Hi Adwin Jha,

    You could choose a hash algorithm to hash  any kinds of file , get the hashed string and then use the code.

    string strCode = //write your hashed string of file
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);
    
                // qrcode.GetGraphic 
                Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
                qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg"));//write your path where you want to store the qr-code image.
    
    
              

    About how to hash a file , you could refer to

    https://www.kunal-chowdhury.com/2016/01/compute-hash-code.html

    Best regards,

    Ackerly Xu

    Tuesday, December 18, 2018 6:41 AM
  • User1997423929 posted

    Sorry, hash algorithm was also not worked. Any other solution because I want to scan all the doc file(i.e. text, PDF, word) and image file too.

    Tuesday, December 18, 2018 11:22 AM
  • User-893317190 posted

    Hi Adwin Jha,

    Below is my code using hash algorithm and it could hash doc,image files.

    The hashed string could generate QR-Code.

    Below is my code.

     protected void Page_Load(object sender, EventArgs e)
            {
    
                string strCode =  GetHashCode(Server.MapPath("/csharpDemo2/ele.gif"), new SHA256CryptoServiceProvider());
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);
    
         
                Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
          
                
    
                //qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg"));
    
                MemoryStream ms = new MemoryStream();
                qrCodeImage.Save(ms, ImageFormat.Jpeg);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "image/Jpeg";
              HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                HttpContext.Current.Response.End();
    
    
    
            }
    
            string GetHashCode(string filePath, HashAlgorithm cryptoService)
            {
                // create or use the instance of the crypto service provider
                // this can be either MD5, SHA1, SHA256, SHA384 or SHA512
                using (cryptoService)
                {
                    using (var fileStream = new FileStream(filePath,
                                                           FileMode.Open,
                                                           FileAccess.Read,
                                                           FileShare.ReadWrite))
                    {
                        var hash = cryptoService.ComputeHash(fileStream);
                        var hashString = Convert.ToBase64String(hash);
                        return hashString.TrimEnd('=');
                    }
                }
            }

    Best regards,

    Ackerly Xu

    Wednesday, December 19, 2018 1:48 AM
  • User1997423929 posted

    Hi Adwin Jha,

    Below is my code using hash algorithm and it could hash doc,image files.

    The hashed string could generate QR-Code.

    Below is my code.

     protected void Page_Load(object sender, EventArgs e)
            {
    
                string strCode =  GetHashCode(Server.MapPath("/csharpDemo2/ele.gif"), new SHA256CryptoServiceProvider());
                QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                QRCode qrcode = new QRCode(qrCodeData);
    
         
                Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
          
                
    
                //qrCodeImage.Save(Server.MapPath("/csharpDemo2/code.jpg"));
    
                MemoryStream ms = new MemoryStream();
                qrCodeImage.Save(ms, ImageFormat.Jpeg);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "image/Jpeg";
              HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                HttpContext.Current.Response.End();
    
    
    
            }
    
            string GetHashCode(string filePath, HashAlgorithm cryptoService)
            {
                // create or use the instance of the crypto service provider
                // this can be either MD5, SHA1, SHA256, SHA384 or SHA512
                using (cryptoService)
                {
                    using (var fileStream = new FileStream(filePath,
                                                           FileMode.Open,
                                                           FileAccess.Read,
                                                           FileShare.ReadWrite))
                    {
                        var hash = cryptoService.ComputeHash(fileStream);
                        var hashString = Convert.ToBase64String(hash);
                        return hashString.TrimEnd('=');
                    }
                }
            }

    Best regards,

    Ackerly Xu

    Sorry My problem has not solved yet. Actually, when we are converting the file path(by using has), QR Code are successfully generated, but when I scan that QR Code it returns the hash string in text.

    Wednesday, December 19, 2018 9:33 AM
  • User-893317190 posted

    Hi Adwin Jha,

    You could read the link below to learn how to get text content from pdf, word in c#

    https://www.c-sharpcorner.com/blogs/reading-contents-from-pdf-word-text-files-in-c-sharp1

    About image, I suggest you could try to convert your image into base64 string and use the string to generate qr code,but because the string's length is limited and data of the image is large, it is hard to convert image to qr code.

    https://www.codeproject.com/Questions/1118286/How-do-I-convert-image-into-QR-code-in-Csharp

    Best regards,

    Ackerly Xu

    Thursday, December 20, 2018 7:04 AM