Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1280
Creating QR barcode from code behind and saving to disk
posted

Hello,

I am trying to create a QR code from codebehind and save it to the disk without showing it on any GUI.

But it just saves an empty image to the disk, with a size of 169 kb.

Here's the code:

var qrCode = new XamQRCodeBarcode
            {
                Data = data,
                BarsFillMode = BarsFillMode.EnsureEqualSize,
                Stretch = Stretch.None,
                Width = 100,
                Height = 100,
                ErrorCorrectionLevel = QRCodeErrorCorrectionLevel.Medium,
                SizeVersion = SizeVersion.Undefined,
                Fnc1Mode = Fnc1Mode.None,
                EncodingMode = EncodingMode.Undefined,
                EciNumber = 3,
                ApplicationIndicator = ""
            };

            var width = (int)qrCode.Width;
            var height = (int)qrCode.Height;

            var renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            renderTargetBitmap.Render(qrCode);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

            if (!Directory.Exists(@"C:\Temp\Images\"))
                Directory.CreateDirectory(@"C:\Temp\Images\");
            using (var stream = new FileStream(@"C:\Temp\Images\" + data + ".png", FileMode.Create))
            {
                encoder.Save(stream);
            }

The above code only creates an empty image, then I also tried to use the same code as the infragistic sample without using the XAML, same result empty image.

var saveStream = new SaveFileDialog { Filter = "PNG (*.png) | *.png" };
            var qrCode = new XamQRCodeBarcode
            {
                Data = _page.TxbData.Text,
                BarsFillMode = BarsFillMode.EnsureEqualSize,
                Stretch = Stretch.None,
                Width = 100,
                Height = 100,
                ErrorCorrectionLevel = QRCodeErrorCorrectionLevel.Medium,
                SizeVersion = SizeVersion.Undefined,
                Fnc1Mode = Fnc1Mode.None,
                EncodingMode = EncodingMode.Undefined,
                EciNumber = 3,
                ApplicationIndicator = ""
            };

            var border = new Border
            {
                BorderBrush = Brushes.Red,
                BorderThickness = new Thickness(1),
                Child = qrCode,
                Margin = new Thickness(10)
            };

            _page.StPnlBarcode.Children.Add(border);
            
            var width = (int)qrCode.Width;
            var height = (int)qrCode.Height;

            var renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            renderTargetBitmap.Render(qrCode);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

           
            if (saveStream.ShowDialog() == false) return;

            using (var stream = saveStream.OpenFile())
            {
                encoder.Save(stream);
            }

The code above creates the QR code and shows in the stackpanel StPnlBarcode, but the image is still empty when saved to disk.

Parents
  • 2406
    posted

    Hi Nawed,

    As I read from here, the control needs to be in the Visual Tree for RenderTargetBitmap to see it. A possible resolution would be to host it temporarily inside a 0 opacity container as mentioned here.

Reply Children