Пример #1
0
        public static Bitmap ToBitmap(BitMatrix bitMatrix, int Margin = 0)
        {
            PixelDataRenderer renderer = new PixelDataRenderer();
            var options = new EncodingOptions()
            {
                Margin = Margin
            };
            var pixelData = renderer.Render(bitMatrix, BarcodeFormat.QR_CODE, null, options);

            return(ToBitmap(pixelData));
        }
Пример #2
0
        public byte[] Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options)
        {
            PixelData pixelData = _dataRenderer.Render(matrix, format, content, options);

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

                    byte[] source = new byte[bitmapData.Stride * matrix.Height];

                    var num1 = matrix.Height + 16 > _defaultTextFont.Height ? _defaultTextFont.Height : 0;

                    pixelData.Pixels.CopyTo(source, 0);

                    try
                    {
                        if (!options.PureBarcode && num1 > 0)
                        {
                            IsPureBarcodeWithText(matrix.Height, matrix.Width, bitmapData.Stride - (4 * matrix.Width), source, num1);
                        }

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

                    if (!options.PureBarcode && num1 > 0)
                    {
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            switch (format)
                            {
                            case BarcodeFormat.EAN_8:
                                if (content.Length < 8)
                                {
                                    content = OneDimensionalCodeWriter.CalculateChecksumDigitModulo10(content);
                                }
                                content = content.Insert(4, "   ");
                                break;

                            case BarcodeFormat.EAN_13:
                                if (content.Length < 13)
                                {
                                    content = OneDimensionalCodeWriter.CalculateChecksumDigitModulo10(content);
                                }
                                content = content.Insert(7, "   ");
                                content = content.Insert(1, "   ");
                                break;
                            }

                            SolidBrush   solidBrush = new SolidBrush(Color.FromArgb(_dataRenderer.Foreground.A, _dataRenderer.Foreground.R, _dataRenderer.Foreground.G, _dataRenderer.Foreground.B));
                            StringFormat format1    = new StringFormat()
                            {
                                Alignment = StringAlignment.Center
                            };
                            graphics.DrawString(string.IsNullOrEmpty(_displayText) ? content : _displayText, _defaultTextFont, (Brush)solidBrush, (float)(1 * matrix.Width / 2), (float)(options.Height - num1), format1);
                        }
                    }

                    bitmap.Save(memoryStream, ImageFormat.Png);

                    return(memoryStream.ToArray());
                }
        }
        public PixelData ToBitmap(BarcodeFormat format, String content)
        {
            var renderer = new PixelDataRenderer();

            return(renderer.Render(this, format, content));
        }
        public PixelData ToBitmap()
        {
            var renderer = new PixelDataRenderer();

            return(renderer.Render(this, BarcodeFormat.EAN_8, string.Empty));
        }