ManuelKetisch
ManuelKetisch

Reputation: 53

How to generate vector qr codes and display them in a wpf image control?

I edited the initial post

i want to creat a small c# desktop app for my company. The app should generate vector QR-Code and display the generated QR in an image control of a WPF window.

I am using the QRCoder library for this and already implemented it. I have also created the WPF controls for it and wrote the following lines into the a button_click event.

Edit My code currently looks like this:

{
    public partial class MainWindow : Window
    {
        private Image qrCodeAsXaml;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnGenerate_Click(object sender, RoutedEventArgs e)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(inputBox.Text, QRCodeGenerator.ECCLevel.H);
            XamlQRCode qrCode = new XamlQRCode(qrCodeData);
            DrawingImage qrCodeAsXaml = qrCode.GetGraphic(20);
        }

        private void QrImage_SourceUpdated(object sender, DataTransferEventArgs e)
        {
            this.qrImage.Source = qrCodeAsXaml;
        }
    }
}

In the MainWindow.xaml the Image control part looks like this:

<Image x:Name="qrImage" Binding.SourceUpdated="QrImage_SourceUpdated" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0"/>

In...

private void QrImage_SourceUpdated(object sender, DataTransferEventArgs e)
        {
            this.qrImage.Source = qrCodeAsXaml;
        }

I am beeing told that i cannot implicitly convert type 'System.Windows.Controls.Image' to 'System.Windows.Media.ImageSource'

So i need to explicitly convert that, but i don't have a clue how this is done.

Any suggestions?

Upvotes: 1

Views: 6519

Answers (2)

ManuelKetisch
ManuelKetisch

Reputation: 53

Ok i finaly got it working thanks to you Andy!

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void BtnGenerate_Click(object sender, RoutedEventArgs e)
    {
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(inputBox.Text, QRCodeGenerator.ECCLevel.H);
        XamlQRCode qrCode = new XamlQRCode(qrCodeData);
        DrawingImage qrCodeAsXaml = qrCode.GetGraphic(20);
        qrImage.Source = qrCodeAsXaml;
    }
}

xaml:

<Image x:Name="qrImage" Binding.SourceUpdated="BtnGenerate_Click" 
           HorizontalAlignment="Center" VerticalAlignment="Center" 
           Grid.Column="0" Grid.Row="0" 
           Width="Auto" MaxWidth="350" Height="Auto" MaxHeight="350"/>

Thank you for helping out!

Upvotes: 1

Andy
Andy

Reputation: 12276

Don't use svg format, use drawingimage.

https://github.com/codebude/QRCoder/wiki/Advanced-usage---QR-Code-renderers#28-xamlqrcode-renderer-in-detail

Set the source of your image to that.

Upvotes: 3

Related Questions