Example #1
0
 public static byte[] GetQRCode(string txt, ECCLevel eccLevel, int size)
 {
     using (var qrGen = new QRCodeGenerator())
         using (var qrCode = qrGen.CreateQrCode(txt, eccLevel))
             using (var qrPng = new PngByteQRCode(qrCode))
                 return(qrPng.GetGraphic(size));
 }
Example #2
0
 public static byte[] GetQRCode(string plainText, int pixelsPerModule, byte[] darkColorRgba, byte[] lightColorRgba, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1)
 {
     using (var qrGenerator = new QRCodeGenerator())
         using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
             using (var qrCode = new PngByteQRCode(qrCodeData))
                 return(qrCode.GetGraphic(pixelsPerModule, darkColorRgba, lightColorRgba));
 }
Example #3
0
 public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size, bool drawQuietZones = true)
 {
     using (var qrGen = new QRCodeGenerator())
         using (var qrCode = qrGen.CreateQrCode(txt, eccLevel))
             using (var qrPng = new PngByteQRCode(qrCode))
                 return(qrPng.GetGraphic(size, drawQuietZones));
 }
Example #4
0
        /// <summary>
        /// Creates a PDF document with given colors DPI and quality
        /// </summary>
        /// <param name="pixelsPerModule"></param>
        /// <param name="darkColorHtmlHex"></param>
        /// <param name="lightColorHtmlHex"></param>
        /// <param name="dpi"></param>
        /// <param name="jpgQuality"></param>
        /// <returns></returns>
        public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, int dpi = 150, long jpgQuality = 85)
        {
            byte[] jpgArray     = null, pngArray = null;
            var    imgSize      = QrCodeData.ModuleMatrix.Count * pixelsPerModule;
            var    pdfMediaSize = (imgSize * 72 / dpi).ToString(CultureInfo.InvariantCulture);

            //Get QR code image
            using (var qrCode = new PngByteQRCode(QrCodeData)) pngArray = qrCode.GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));

            //Create image and transofrm to JPG
            using (var msPng = new MemoryStream())
            {
                msPng.Write(pngArray, 0, pngArray.Length);
                var img = Image.FromStream(msPng);
                using (var msJpeg = new MemoryStream())
                {
                    // Create JPEG with specified quality
                    var jpgImageCodecInfo    = ImageCodecInfo.GetImageEncoders().First(x => x.MimeType == "image/jpeg");
                    var jpgEncoderParameters = new EncoderParameters(1)
                    {
                        Param = new[] { new EncoderParameter(Encoder.Quality, jpgQuality) }
                    };
                    img.Save(msJpeg, jpgImageCodecInfo, jpgEncoderParameters);
                    jpgArray = msJpeg.ToArray();
                }
            }

            //Create PDF document
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream, Encoding.GetEncoding("ASCII"));

                var xrefs = new List <long>();

                writer.Write("%PDF-1.5\r\n");
                writer.Flush();

                stream.Write(pdfBinaryComment, 0, pdfBinaryComment.Length);
                writer.WriteLine();

                writer.Flush();
                xrefs.Add(stream.Position);

                writer.Write(
                    xrefs.Count + " 0 obj\r\n" +
                    "<<\r\n" +
                    "/Type /Catalog\r\n" +
                    "/Pages 2 0 R\r\n" +
                    ">>\r\n" +
                    "endobj\r\n"
                    );

                writer.Flush();
                xrefs.Add(stream.Position);

                writer.Write(
                    xrefs.Count + " 0 obj\r\n" +
                    "<<\r\n" +
                    "/Count 1\r\n" +
                    "/Kids [ <<\r\n" +
                    "/Type /Page\r\n" +
                    "/Parent 2 0 R\r\n" +
                    "/MediaBox [0 0 " + pdfMediaSize + " " + pdfMediaSize + "]\r\n" +
                    "/Resources << /ProcSet [ /PDF /ImageC ]\r\n" +
                    "/XObject << /Im1 4 0 R >> >>\r\n" +
                    "/Contents 3 0 R\r\n" +
                    ">> ]\r\n" +
                    ">>\r\n" +
                    "endobj\r\n"
                    );

                var X = "q\r\n" +
                        pdfMediaSize + " 0 0 " + pdfMediaSize + " 0 0 cm\r\n" +
                        "/Im1 Do\r\n" +
                        "Q";

                writer.Flush();
                xrefs.Add(stream.Position);

                writer.Write(
                    xrefs.Count + " 0 obj\r\n" +
                    "<< /Length " + X.Length + " >>\r\n" +
                    "stream\r\n" +
                    X + "endstream\r\n" +
                    "endobj\r\n"
                    );

                writer.Flush();
                xrefs.Add(stream.Position);

                writer.Write(
                    xrefs.Count + " 0 obj\r\n" +
                    "<<\r\n" +
                    "/Name /Im1\r\n" +
                    "/Type /XObject\r\n" +
                    "/Subtype /Image\r\n" +
                    "/Width " + imgSize + "/Height " + imgSize + "/Length 5 0 R\r\n" +
                    "/Filter /DCTDecode\r\n" +
                    "/ColorSpace /DeviceRGB\r\n" +
                    "/BitsPerComponent 8\r\n" +
                    ">>\r\n" +
                    "stream\r\n"
                    );
                writer.Flush();
                stream.Write(jpgArray, 0, jpgArray.Length);
                writer.Write(
                    "\r\n" +
                    "endstream\r\n" +
                    "endobj\r\n"
                    );

                writer.Flush();
                xrefs.Add(stream.Position);

                writer.Write(
                    xrefs.Count + " 0 obj\r\n" +
                    jpgArray.Length + " endobj\r\n"
                    );

                writer.Flush();
                var startxref = stream.Position;

                writer.Write(
                    "xref\r\n" +
                    "0 " + (xrefs.Count + 1) + "\r\n" +
                    "0000000000 65535 f\r\n"
                    );

                foreach (var refValue in xrefs)
                {
                    writer.Write(refValue.ToString("0000000000") + " 00000 n\r\n");
                }

                writer.Write(
                    "trailer\r\n" +
                    "<<\r\n" +
                    "/Size " + (xrefs.Count + 1) + "\r\n" +
                    "/Root 1 0 R\r\n" +
                    ">>\r\n" +
                    "startxref\r\n" +
                    startxref + "\r\n" +
                    "%%EOF"
                    );

                writer.Flush();

                stream.Position = 0;

                return(stream.ToArray());
            }
        }