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.
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.
Hi,
In other words, it's not possible to do it from code behind.
In my case there is no UI, it's just a class which reads a excel file and creates images with QR code.
I succeeded to save a qr code created by code behind when measure and arrange it before passing it to the RenderTargetBitmap, i.e.:
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 = "" }; width = (int)qrCode.Width; height = (int)qrCode.Height; qrCode.Measure(new Size(width, height)); qrCode.Arrange(new Rect(0, 0, width, height)); // save image var renderTargetBitmap = new RenderTargetBitmap((int)qrCode.ActualWidth, (int)qrCode.ActualHeight, 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); stream.Flush(); stream.Close(); } // revert old position qrCode.Measure(new Size());
Is it an option for you?
Yes, works fine...
Thank you
I'm glad it helps.
Let us know if you have further questions.