PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] How to display Bitmap object in WinUI 3 application

 July 30, 2022     .net, c#, image, qrcoder, winui-3     No comments   

Issue

I want to display QR code generated by QRCoder library ( https://github.com/codebude/QRCoder/ ) in my WinUI 3 desktop app.

From QRCoder I get System.Drawing.Bitmap object:

            QRCodeGenerator qrCodeGenerator = new();
            QRCodeData qrCodeData = qrCodeGenerator.CreateQrCode(associateSoftwareTokenResponse.SecretCode, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new(qrCodeData);
            Bitmap qrCodeBitmap = qrCode.GetGraphic(20);

Then assigning it to XAML Image control: qrCodeImage.Source = qrCodeBitmap gives an error:

Error CS0029 Cannot implicitly convert type 'System.Drawing.Bitmap' to 'Microsoft.UI.Xaml.Media.ImageSource'

So apparently there is still some conversion needed.

All documentation and examples I managed to find explain how to print an image from file but not Bitmap object.

How can I display this code generated Bitmap in my WinUI 3 app?


Solution

You should be able to create a BitmapImage from a stream something like this:

Bitmap qrCodeBitmap = ...;
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream stream = new MemoryStream())
{
    qrCodeBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    stream.Position = 0;
    bitmapImage.SetSource(stream.AsRandomAccessStream());
}
image.Source = bitmapImage;


Answered By - mm8
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing