Example #1
0
        private void BarCodeListGenerator(BarcodeDto data)
        {
            var barcodeWriterPixelData = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.CODE_128,
                Options = new ZXing.Common.EncodingOptions
                {
                    Height = 280,
                    Width  = 280,
                    Margin = 2,
                }
            };


            foreach (var code in data.Code)
            {
                var pixelData = barcodeWriterPixelData.Write(code);

                Bitmap bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb);

                MemoryStream memoryStream = new MemoryStream();

                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

                System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);

                bitmap.Save(memoryStream, ImageFormat.Png);

                byte[] byteImage = memoryStream.ToArray();

                SaveBarcode(code, byteImage);
            }
        }
Example #2
0
        private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (_text == null)
            {
                return;
            }

            var writer = new BarcodeWriterPixelData();

            writer.Options.Hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H;
            writer.Options.Width  = 768;
            writer.Options.Height = 768;
            writer.Format         = BarcodeFormat.QR_CODE;

            var data   = writer.Write(_text);
            var bitmap = CanvasBitmap.CreateFromBytes(sender, data.Pixels, data.Width, data.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized);

            args.DrawingSession.Transform = System.Numerics.Matrix3x2.CreateScale(256f / 768f);

            args.DrawingSession.DrawImage(bitmap);

            if (_overlay != null)
            {
                args.DrawingSession.DrawImage(_overlay, new System.Numerics.Vector2((data.Width - _overlay.SizeInPixels.Width) / 2f, (data.Height - _overlay.SizeInPixels.Height) / 2f));
            }
        }
Example #3
0
        static void barcode(String x, int h, int w, int m, BarcodeFormat bf, string outpath)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
            {
                Format  = bf,//BarcodeFormat.CODE_128,
                Options = new EncodingOptions
                {
                    Height      = h,
                    Width       = w,
                    PureBarcode = false, // this should indicate that the text should be displayed, in theory. Makes no difference, though.
                    Margin      = m
                }
            };
            var pixelData = writer.Write(x);

            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                try
                {
                    System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }
                bitmap.Save(outpath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
Example #4
0
        public static byte[] ToBarcodeBytes(this string context, Size size)
        {
            var writer = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Width        = size.Width,
                    Height       = size.Height,
                    CharacterSet = "UTF-8",

                    /*
                     * Error Correction Level
                     * L:  7%
                     * M: 15%
                     * Q: 25%
                     * H: 30%
                     */
                    ErrorCorrection = ErrorCorrectionLevel.H
                },
                Renderer = new PixelDataRenderer()
                {
                    Foreground = PixelDataRenderer.Color.Black,
                    Background = PixelDataRenderer.Color.White
                }
            };

            return(writer.Write(context).Pixels);
        }
Example #5
0
        public void GeneratePromptPayQrCode(string path, string filename, int width = 200, int height = 200, int margin = 5)
        {
            var barcodeWriter = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions
                {
                    Height = height,
                    Width  = width,
                    Margin = margin
                }
            };
            var pixelData = barcodeWriter.Write(PromptPayPayload);

            using (var fileStream = new FileStream(path + filename + ".jpg", FileMode.CreateNew))
                using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
                {
                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                                     ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                     pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    bitmap.Save(fileStream, ImageFormat.Jpeg);
                    fileStream.Flush();
                }
        }
Example #6
0
        private string GenerateBarcode(string apiAddr)
        {
            var writer = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions {
                    Width = 400, Height = 400, Margin = 1
                }
            };

            var result    = writer.Write(apiAddr);
            var base64str = string.Empty;

            using (var bitmap = new System.Drawing.Bitmap(result.Width, result.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                using (var ms = new System.IO.MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, result.Width, result.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        System.Runtime.InteropServices.Marshal.Copy(result.Pixels, 0, bitmapData.Scan0, result.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    // PNG or JPEG or whatever you want
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    base64str = Convert.ToBase64String(ms.ToArray());
                }
            }
            return(base64str);
        }
Example #7
0
        /// <summary>
        /// 二维码
        /// </summary>
        public byte[] GetQrCodeBytes(string content,
                                     int size = QRCODE_SIZE)
        {
            content = ConvertHelper.GetString(content);

            var option = new QrCodeEncodingOptions()
            {
                CharacterSet    = this.Charset,
                DisableECI      = true,
                ErrorCorrection = this.ErrorCorrectionLevel ?? ErrorCorrectionLevel.H,
                Width           = size,
                Height          = size,
                Margin          = this.Margin
            };

            var writer = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = option
            };

            var pix = writer.Write(content);

            return(pix.Pixels);
        }
Example #8
0
        /// <summary>
        /// 条码
        /// </summary>
        public byte[] GetBarCodeBytes(string content,
                                      int width = BARCODE_SIZE_WIDTH, int height = BARCODE_SIZE_HEIGHT)
        {
            content = ConvertHelper.GetString(content);

            var options = new QrCodeEncodingOptions()
            {
                CharacterSet = this.Charset,
                Width        = width,
                Height       = height,
                Margin       = this.Margin,
                // 是否是纯码,如果为 false,则会在图片下方显示数字
                PureBarcode = false,
            };

            var writer = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.CODE_128,
                Options = options
            };

            var pix = writer.Write(content);

            return(pix.Pixels);
        }
Example #9
0
        public static Bitmap GetQRCode(string strContent, int width = 200, int height = 200)
        {
            //二维码生成开始
            BitMatrix bitMatrix;//定义像素矩阵对象

            bitMatrix = new MultiFormatWriter().encode(strContent, BarcodeFormat.QR_CODE /*条码或二维码标准*/, width /*宽度*/, height /*高度*/);
            var bw = new BarcodeWriterPixelData();

            var pixelData = bw.Write(bitMatrix);
            var bitmap    = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);//预绘制32bit标准的位图片

            var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height) /*绘制矩形区域及偏移量*/,
                                             System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            try
            {
                //假设位图的 row stride = 4 字节 * 图片的宽度
                System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);//从内存 Unlock bitmap 对象
            }
            //二维码生成结束
            return(bitmap);
        }
Example #10
0
        public static byte[] GenerateCodePng(string Text, int Width, int Height)
        {
            BarcodeWriterPixelData Writer = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions()
                {
                    Width  = Width,
                    Height = Height
                }
            };

            PixelData QrCode = Writer.Write(Text);

            Width  = QrCode.Width;
            Height = QrCode.Height;

            int Size = Width * Height << 2;

            using (SKData Data = SKData.Create(Size))
            {
                Marshal.Copy(QrCode.Pixels, 0, Data.Data, Size);

                using (SKImage Result = SKImage.FromPixels(new SKImageInfo(Width, Height, SKColorType.Bgra8888), Data, Width << 2))
                {
                    using (SKData Encoded = Result.Encode(SKEncodedImageFormat.Png, 100))
                    {
                        return(Encoded.ToArray());
                    }
                }
            }
        }
Example #11
0
        private static byte[] CreateQrCodeImage(string content, int height, int width)
        {
            byte[] result;

            var barcodeWriter = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions()
                {
                    Height = height,
                    Width  = width
                }
            };

            var pixelData = barcodeWriter.Write(content);

            using (var image = Image.LoadPixelData <Rgba32>(pixelData.Pixels, pixelData.Width, pixelData.Height))
                using (var ms = new MemoryStream())
                {
                    image.SaveAsPng(ms);
                    result = ms.ToArray();
                }

            return(result);
        }
Example #12
0
        static void Main(string[] args)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
            {
                Format = BarcodeFormat.DATA_MATRIX,
            };

            writer.Options.PureBarcode = true;

            var pixelData = writer.Write("Testando");

            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                using (var ms = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    var base64str = Convert.ToBase64String(ms.ToArray());

                    bitmap.Save("c:\\projetos\\test.png", System.Drawing.Imaging.ImageFormat.Png);

                    File.WriteAllText("c:\\projetos\\codebar.html", string.Format(html, base64str));
                }
            }
        }
Example #13
0
        public byte[] GenerateImageFromCode(string code)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
            {
                Format = BarcodeFormat.QR_CODE
            };
            var pixelData = writer.Write(code);

            using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            {
                using (var ms = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.Save(ms, ImageFormat.Png);
                    return(ms.ToArray());
                }
            }
        }
Example #14
0
        public List <string> Get(List <string> valores)
        {
            var retorno = new List <string>();

            valores.ForEach(valor =>
            {
                BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
                {
                    Format = BarcodeFormat.CODE_128
                };
                writer.Options.PureBarcode = true;
                var pixelData = writer.Write(valor);
                using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
                {
                    var stream     = new MemoryStream();
                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    bitmap.Save(stream, ImageFormat.Png);
                    retorno.Add(Convert.ToBase64String(stream.ToArray()));
                }
            });
            return(retorno);
        }
Example #15
0
        public PixelData Write(string contents)
        {
            var writer = new BarcodeWriterPixelData
            {
                Format  = Format.ToZXing(),
                Options = options.wrappedEncodingOptions
            };

            return(writer.Write(contents).ToInterop());
        }
Example #16
0
        /// <summary>
        /// Generates a PDF417 encoded byte array of a string
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static byte[] GenerateByteArray(string content)
        {
            Guard.ThrowIfNullOrEmpty(content, "Please enter a valid string");

            var height       = 300;
            var width        = 900;
            var margin       = 2;
            var pdf417Writer = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.PDF_417,
                Options = new EncodingOptions
                {
                    Height      = height,
                    Width       = width,
                    Margin      = margin,
                    PureBarcode = true,
                    GS1Format   = true
                }
            };

            var pixelData = pdf417Writer.Write(content);

            Console.WriteLine($"chura:{pixelData.Height}");

            using var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb);
            using var ms     = new MemoryStream();
            // lock the data area for fast access
            var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                             ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

            try
            {
                // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                                            pixelData.Pixels.Length);
            }
            catch
            {
                throw new ClientFriendlyException($"Failed to generate barcode");
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }

            bitmap.Save(ms, ImageFormat.Png);

            return(ms.ToArray());
        }
Example #17
0
        private byte[] CreateBarcode(string content)
        {
            // 使用ITF 格式,不能被现在常用的支付宝、微信扫出来
            // 如果想生成可识别的可以使用 CODE_128 格式
            var qrCodeWriter = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.CODE_128,
                Options = new Code128EncodingOptions()
                {
                    Height      = _height,
                    Width       = _width,
                    Margin      = _margin,
                    PureBarcode = _showContent,
                    Hints       =
                    {
                        { EncodeHintType.CHARACTER_SET,    "UTF-8" },
                        { EncodeHintType.ERROR_CORRECTION, _level  }
                    }
                }
            };

            var pixelData = qrCodeWriter.Write(content);

            using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                using (var ms = new MemoryStream())
                {
                    var bitmapData =
                        bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                                                    pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.MakeTransparent();

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    return(ms.ToArray());
                }
            }
        }
Example #18
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var content = context.AllAttributes["content"].ToString();
            //var width = int.Parse(context.AllAttributes["width"].ToString());
            //var height = int.Parse(context.AllAttributes["height"].ToString());
            var width  = 500;
            var height = 300;

            var codeWriter = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width  = width,
                    Margin = 0
                }
            };

            var pixeldata = codeWriter.Write(content);

            using (var bitmap = new Bitmap(pixeldata.Width, pixeldata.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                using (var memorystream = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(
                        new Rectangle(0, 0, pixeldata.Width, pixeldata.Height),
                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixeldata.Pixels, 0, bitmapData.Scan0, pixeldata.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.Save(memorystream, System.Drawing.Imaging.ImageFormat.Png);
                    output.TagName = "img";
                    output.Attributes.Clear();
                    output.Attributes.Add("width", width);
                    output.Attributes.Add("height", height);
                    output.Attributes.Add("src", String.Format("data:image/png;base64,{0}", Convert.ToBase64String(memorystream.ToArray())));
                }
            }
        }
Example #19
0
        private MemoryStream GetQrCodeAsBmpImage(MetaData metaData)
        {
            var barcodeWriter = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions()
                {
                    ErrorCorrection = ErrorCorrectionLevel.Q
                }
            };

            var pixelData = barcodeWriter.Write(Encoder.EncodeBase64(metaData.ToBytes()));

            var bmpImage = ImageHelper.PixelDataToBmp(pixelData, 10);

            return(new MemoryStream(bmpImage));
        }
Example #20
0
        static void barcode128(String x)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.CODE_128,
                Options = new EncodingOptions
                {
                    Height      = 80,
                    Width       = 420,
                    PureBarcode = false, // this should indicate that the text should be displayed, in theory. Makes no difference, though.
                    Margin      = 10
                }
            };
            var pixelData = writer.Write(x);

            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                try
                {
                    System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }

                bitmap.Save(@"c:\code\npoi_ex\out\" + x + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

                /*
                 *              using (var ms = new System.IO.MemoryStream())
                 * {
                 *  var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                 *  try
                 *  {
                 *      System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                 *  }
                 *  finally
                 *  {
                 *      bitmap.UnlockBits(bitmapData);
                 *  }
                 *
                 *  return File(ms.ToArray(), "image/jpeg");
                 * }*/
            }
        }
Example #21
0
        public static Bitmap Encode(string value, int width, int height, int margin, Color color1, Color color2)
        {
            var   color1Gray = Utils.GetGrayScale(color1);
            var   color2Gray = Utils.GetGrayScale(color2);
            Color darkColor, lightColor;

            if (color1Gray < color2Gray)
            {
                darkColor  = color1;
                lightColor = color2;
            }
            else
            {
                darkColor  = color2;
                lightColor = color1;
            }

            if (Utils.GetGrayScale(darkColor) >= 128)
            {
                throw new Exception(Constants.NoDarkColorError);
            }

            if (Utils.GetGrayScale(lightColor) < 128)
            {
                throw new Exception(Constants.NoLightColorError);
            }

            var qrCodeWriter = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width  = width,
                    Margin = margin
                }
            };

            var pixelData = qrCodeWriter.Write(value);
            var bitmap    = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb);

            SetBitmapData(bitmap, pixelData.Pixels, color1, color2);

            return(bitmap);
        }
Example #22
0
        private void CreaBarcodeButton_Clicked(object sender, EventArgs e)
        {
            var barcodeWriter = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions
                {
                    Width  = 200,
                    Height = 200,
                    Margin = 0
                }
            };

            PixelData pixelData = barcodeWriter.Write(BarcodeText.Text);

            byte[] bmpArray = BitmapConverter.FromPixelData(pixelData);

            BarcodeImage.Source = ImageSource.FromStream(() => new MemoryStream(bmpArray));
        }
Example #23
0
        public static Bitmap Encode(string value, int width, int height, int margin)
        {
            var qrCodeWriter = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width  = width,
                    Margin = margin
                }
            };

            var pixelData = qrCodeWriter.Write(value);
            var bitmap    = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb);

            SetBitmap(bitmap, pixelData);
            return(bitmap);
        }
Example #24
0
        //https://rajeeshmenoth.wordpress.com/2017/05/11/qr-code-generator-in-asp-net-core-using-zxing-net/
        public byte[] GenerateQr(string token, int width, int height)
        {
            try
            {
                var qrCodeWriter = new BarcodeWriterPixelData()
                {
                    Format  = BarcodeFormat.QR_CODE,
                    Options = new QrCodeEncodingOptions()
                    {
                        Height = height,
                        Width  = width,
                        Margin = 0,
                    }
                };

                var result = qrCodeWriter.Write(token);

                using (var bitmap = new Bitmap(result.Width, result.Height, PixelFormat.Format32bppRgb))
                {
                    using (var ms = new MemoryStream())
                    {
                        var bitmapData = bitmap.LockBits(new Rectangle(0, 0, result.Width,
                                                                       result.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                        try
                        {
                            Marshal.Copy(result.Pixels, 0, bitmapData.Scan0, result.Pixels.Length);
                        }
                        finally
                        {
                            bitmap.UnlockBits(bitmapData);
                        }

                        bitmap.Save(ms, ImageFormat.Png);
                        return(ms.ToArray());
                    }
                }
            }
            catch (Exception e)
            {
                throw new QrCodeException("Não foi possóvel gerar o código QR.", e);
            }
        }
Example #25
0
        public PictureViewModel GenerateQrCodeImage(QrCodeContent qrCodeContent)
        {
            var width        = 250;
            var height       = 250;
            var margin       = 0;
            var qrCodeWriter = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions()
                {
                    Height = height,
                    Width  = width,
                    Margin = margin
                }
            };
            var pixelData = qrCodeWriter.Write($"{(env.IsDevelopment() ? Constants.QrUrlDevelopment : Constants.QrUrl)}/traveling/{qrCodeContent.BusId}");
            var picture   = new PictureViewModel();

            picture.Name      = $"Qr code for {qrCodeContent.BusName}";
            picture.MediaType = "image/jpg";
            picture.Type      = "File";
            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            {
                using (var ms = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                                     ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    bitmap.Save(ms, ImageFormat.Png);
                    picture.Value = Convert.ToBase64String(ms.ToArray());
                }
            }

            return(picture);
        }
Example #26
0
        public byte[] GetQrCode(string message, int width = 400, int height = 400)
        {
            var write = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Height = height,
                    Width  = width
                }
            };
            var ms        = new MemoryStream();
            var pixelData = write.Write(message);

            using (var image = Image.LoadPixelData <Rgba32>(pixelData.Pixels, width, height))
            {
                image.Save(ms, PngFormat.Instance);
            }
            return(ms.ToArray());
        }
Example #27
0
        /// <summary>
        /// Generate QR code from given string with given width and height.
        /// </summary>
        /// <param name="content">Content</param>
        /// <param name="height">Height</param>
        /// <param name="width">Width</param>
        /// <returns>Base64 of the QR code</returns>
        public static string GenerateQrCode(string content, int width = 250, int height = 250)
        {
            var options = new QrCodeEncodingOptions
            {
                DisableECI   = true,
                CharacterSet = "UTF-8",
                Height       = height,
                Width        = width
            };

            var writer = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = options
            };

            var pixelData = writer.Write(content);

            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            {
                using (var stream = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                                     ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                                                    pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.Save(stream, ImageFormat.Png);
                    return(Convert.ToBase64String(stream.ToArray()));
                }
            }
        }
        private SKBitmap CreateQrBitmap(string text, int width, int height)
        {
            var writer = new BarcodeWriterPixelData
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions
                {
                    Width  = width,
                    Height = height
                }
            };

            var data = writer.Write(text);

            using (var bitmap = new Bitmap(data.Width, data.Height, PixelFormat.Format32bppRgb))
                using (var ms = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(
                        new Rectangle(0, 0, data.Width, data.Height),
                        ImageLockMode.WriteOnly,
                        PixelFormat.Format32bppRgb);
                    try
                    {
                        Marshal.Copy(data.Pixels, 0, bitmapData.Scan0, data.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.Save(ms, ImageFormat.Bmp);

                    ms.Seek(0, SeekOrigin.Begin);

                    using (var stream = new SKManagedStream(ms))
                    {
                        return(SKBitmap.Decode(stream));
                    }
                }
        }
Example #29
0
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="content">内容</param>
        /// <returns></returns>
        private byte[] CreateQrCode(string content)
        {
            var qrCodeWriter = new BarcodeWriterPixelData()
            {
                Format  = BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions()
                {
                    Height = _width,
                    Width  = _height,
                    Margin = _margin
                }
            };
            var pixelData = qrCodeWriter.Write(content);

            using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
            {
                using (var ms = new MemoryStream())
                {
                    var bitmapData =
                        bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    try
                    {
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                                                    pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    bitmap.MakeTransparent();

                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    return(ms.ToArray());
                }
            }
        }
Example #30
0
        private byte[] GetBarcode(string input, int?width = 100, int?height = 100, BarcodeFormat format = BarcodeFormat.CODE_128)
        {
            BarcodeWriterPixelData writer = new BarcodeWriterPixelData
            {
                Format  = format,
                Options = new ZXing.Common.EncodingOptions
                {
                    PureBarcode = true,
                    Margin      = 0,
                    Width       = width.Value,
                    Height      = height.Value
                }
            };

            //return Encoding.UTF8.GetBytes(barcodeWriter.Encode(input).ToString());
            ZXing.Rendering.PixelData pixelData = writer.Write(input);
            using (Bitmap bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    // PNG or JPEG or whatever you want
                    bitmap.SetResolution(70F, 70F);
                    bitmap.Save(ms, ImageFormat.Png);
                    return(ms.ToArray());
                }
            }
        }