Divesh Pal
Divesh Pal

Reputation: 448

I am not able see generated QR Code Image in Xamarin using ZXing.NET.Mobile

Please help me ,

I am not able see generated QR Code Image. What am I doing wrong !

I have used Xamarin Forms . I have Simply used a Image to be populated in StackLayout

public class BarcodePage : ContentPage
{
    public BarcodePage ()
    {
        Image img = new Image {
            Aspect = Xamarin.Forms.Aspect.AspectFit
        };


        img.Source = ImageSource.FromStream (() => {
            var writer = new BarcodeWriter {
                Format = BarcodeFormat.QR_CODE,

                Options = new EncodingOptions {
                    Height = 200,
                    Width = 600
                }
            };
            var bitmap = writer.Write ("My Content");

            MemoryStream ms = new MemoryStream ();
            bitmap.Compress (Bitmap.CompressFormat.Jpeg, 100, ms);
            return ms;
        });

        var Layout = new StackLayout {
            Children = 
            { img}
        };

        Content = Layout;

}

Upvotes: 1

Views: 1046

Answers (1)

Alex Wiese
Alex Wiese

Reputation: 8370

As you are writing the Bitmap data to the MemoryStream using the Compress() method the position of the stream will be at the end when you return it.

Make sure to reset the stream's position before returning it by adding this line.

ms.Position = 0;

The Image will now read the stream from the beginning, rather than from the end.

Upvotes: 1

Related Questions