我想显示 QRCoder 库生成的二维码(https://github.com/codebude/QRCoder/)在我的 WinUI 3 桌面应用程序中。
从 QRCoder 我得到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);
然后将其分配给XAMLImage控制:qrCodeImage.Source = qrCodeBitmap
给出错误:
错误 CS0029 无法将类型“System.Drawing.Bitmap”隐式转换为
'Microsoft.UI.Xaml.Media.ImageSource'
所以显然仍然需要一些转换。
我设法找到的所有文档和示例都解释了如何从文件而不是位图对象打印图像。
如何在我的 WinUI 3 应用程序中显示此代码生成的位图?
您应该能够创建一个BitmapImage
来自这样的流:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)