v2rayN/v2rayN/v2rayN.Desktop/Views/QrcodeView.axaml.cs
2dust cb5069bcfc
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run
Optimize and improve GlobalUsings
2025-10-19 14:13:26 +08:00

44 lines
920 B
C#

namespace v2rayN.Desktop.Views;
public partial class QrcodeView : UserControl
{
public QrcodeView()
{
InitializeComponent();
}
public QrcodeView(string? url)
{
InitializeComponent();
txtContent.Text = url;
imgQrcode.Source = GetQRCode(url);
txtContent.GotFocus += (_, _) => Dispatcher.UIThread.Post(() => { txtContent.SelectAll(); });
}
private Bitmap? GetQRCode(string? url)
{
try
{
var bytes = QRCodeUtils.GenQRCode(url);
return ByteToBitmap(bytes);
}
catch (Exception ex)
{
Logging.SaveLog("GetQRCode", ex);
return null;
}
}
private Bitmap? ByteToBitmap(byte[]? bytes)
{
if (bytes is null)
{
return null;
}
using var ms = new MemoryStream(bytes);
return new Bitmap(ms);
}
}