Beispiel #1
0
        void how_to_print_page_handle(object sender, PrintPageEventArgs e)
        {
            int h = (int)(e.PageSettings.PrintableArea.Height);
            int w = (int)(e.PageSettings.PrintableArea.Width);

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);

            float side = e.PageSettings.PrintableArea.Width;

            side = 100; //=> 1"
            if (e.PageSettings.PrintableArea.Height < e.PageSettings.PrintableArea.Width)
            {
                side = e.PageSettings.PrintableArea.Height;
            }
            float modesize = side / 100 * e.Graphics.DpiX;

            QrCode qrCode = qrEncoder.Encode("0123456789");


            //ISizeCalculation iSizeCal = new FixedModuleSize(2, QuietZoneModules.Zero);
            // This works with the Brady on a very small label
            ISizeCalculation iSizeCal = new FixedCodeSize((int)modesize - 16, QuietZoneModules.Zero);

            DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(iSizeCal,
                                                                      System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.White);

            string test = "test1";

            if (test == "test1")
            {
                MemoryStream mem_stream = new MemoryStream();
                dRenderer.WriteToStream(qrCode.Matrix, ImageFormatEnum.BMP, mem_stream);

                Bitmap bitmap = new Bitmap(mem_stream);
                bitmap.SetResolution(e.Graphics.DpiX, e.Graphics.DpiY);
                //Graphics graphics = Graphics.FromImage(bitmap);
                e.Graphics.DrawImage(bitmap, 0, 0);
            }
            else
            {
                System.Windows.Point dpipoint     = new System.Windows.Point(e.Graphics.DpiX, e.Graphics.DpiY);
                BitmapSource         bitmapsource = dRenderer.WriteToBitmapSource(qrCode.Matrix, dpipoint);
                BitmapFrame          bitmapframe  = BitmapFrame.Create(bitmapsource);

                BitmapEncoder bitmapencoder = new BmpBitmapEncoder();
                bitmapencoder.Frames.Add(bitmapframe);

                MemoryStream mem_stream = new MemoryStream();
                bitmapencoder.Save(mem_stream);

                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(mem_stream);
                e.Graphics.DrawImage(bitmap, 0, 0);
            }
        }
Beispiel #2
0
        Bitmap encodeToBitMap(string data,
                              double dimension_inches,
                              float dpi_x = 600,
                              float dpi_y = 600,
                              ErrorCorrectionLevel correction_level = ErrorCorrectionLevel.L,
                              QuietZoneModules quite_zone           = QuietZoneModules.Zero
                              )
        {
            QrEncoder qrEncoder = new QrEncoder(correction_level);
            QrCode    qrCode    = qrEncoder.Encode(data);

            // Calculate number of pixels.  Note we use dpi in x direction
            // but we should probably use whichever is lowest
            int pixels = (int)(dimension_inches * dpi_x);

            // Check whether we have enough space
            //if (pixels < qrCode.Matrix.Width)
            //throw new Exception("Too small");

            ISizeCalculation iSizeCal = new FixedCodeSize(pixels, quite_zone);

            DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(iSizeCal, System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.White);
            //DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(iSizeCal, System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.LightGray);


            MemoryStream mem_stream = new MemoryStream();

            dRenderer.WriteToStream(qrCode.Matrix, ImageFormatEnum.BMP, mem_stream);
            Bitmap bitmap = new Bitmap(mem_stream);

            bitmap.SetResolution(dpi_x, dpi_y);

            // A different way to do the same.  Just incase the bitmap.SetResolution function does not work
            //System.Windows.Point dpipoint = new System.Windows.Point(dpi, dpi);
            //BitmapSource bitmapsource = dRenderer.WriteToBitmapSource(qrCode.Matrix, dpipoint);
            //MemoryStream outStream = new MemoryStream();
            //BitmapEncoder bitmapencoder = new BmpBitmapEncoder();
            //BitmapFrame bitmapframe = BitmapFrame.Create(bitmapsource);
            //bitmapencoder.Frames.Add(bitmapframe);
            //bitmapencoder.Save(outStream);
            //Bitmap bitmap = new System.Drawing.Bitmap(outStream);

            return(bitmap);
        }
        private int CalculateSuitableWidth(int width, int bitMatrixWidth)
        {
            FixedCodeSize isize = new FixedCodeSize(width, m_QuietZoneModule);
            DrawingSize   dSize = isize.GetSize(bitMatrixWidth);
            int           gap   = dSize.CodeWidth - dSize.ModuleSize * (bitMatrixWidth + 2 * (int)m_QuietZoneModule);

            if (gap == 0)
            {
                return(width);
            }
            else if (dSize.CodeWidth / gap < 6)
            {
                return((dSize.ModuleSize + 1) * (bitMatrixWidth + 2 * (int)m_QuietZoneModule));
            }
            else
            {
                return(dSize.ModuleSize * (bitMatrixWidth + 2 * (int)m_QuietZoneModule));
            }
        }
Beispiel #4
0
        private void PaintQR(string str)
        {
            var graphics = this.CreateGraphics();

            graphics.Clear(Color.White);
            if (string.IsNullOrEmpty(str))
            {
                graphics.DrawString("Clipboard text is empty.", SystemFonts.DefaultFont, Brushes.Black, graphics.VisibleClipBounds);
                return;
            }
            try
            {
                QrEncoder        qrEncoder  = new QrEncoder(ErrorCorrectionLevel.L);
                QrCode           qrCode     = qrEncoder.Encode(str);
                var              moduleSize = new FixedCodeSize(Math.Min((int)graphics.VisibleClipBounds.Width, (int)graphics.VisibleClipBounds.Height), QuietZoneModules.Two);
                GraphicsRenderer render     = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);
                render.Draw(graphics, qrCode.Matrix);
            }
            catch (Exception e)
            {
                graphics.DrawString(e.Message, SystemFonts.DefaultFont, Brushes.Black, graphics.VisibleClipBounds);
            }
        }
Beispiel #5
0
        /*
         * Создаёт QR код с известным текстом.
         */
        static void QRcode()
        {
            string new_file_name = "qrTest";

            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode    qrCode    = new QrCode();

            qrEncoder.TryEncode("Hello, QRWord", out qrCode);
            var fCodeSize = new FixedCodeSize(200, QuietZoneModules.Two);

            fCodeSize.QuietZoneModules = QuietZoneModules.Four;
            GraphicsRenderer renderer = new GraphicsRenderer(fCodeSize, Brushes.Black, Brushes.White);

            MemoryStream ms = new MemoryStream();

            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);

            var imageTemp = new Bitmap(ms);

            var image = new Bitmap(imageTemp, new Size(new Point(200, 200)));

            image.Save(new_file_name + ".png", ImageFormat.Png);
        }