如何在Winui 3应用程序中显示位图对象

发布于 2025-02-05 10:24:49 字数 804 浏览 3 评论 0原文

我想显示QRCoder库生成的QR码()在我的Winui 3桌面应用中。

从qrcoder i获得system.bitmap对象:

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

然后将其分配给XAML image control:qrcodeimage.source = qrcodebitmapmap给出了一个错误:

错误CS0029无法将类型'System.Drawing.BitMap'转换为 'microsoft.ui.xaml.media.imagesource'

,显然仍然需要一些转换。

我设法找到的所有文档和示例说明了如何从文件中打印图像,而不是位图对象。

如何在Winui 3应用中显示此代码生成的位图?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2025-02-12 10:24:49

您应该能够从类似的流中创建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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文