Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Chào anh Long, em muốn làm phần mềm khi quét mã QR nó hiện ra cửa sổ Login trên WPF thì em làm vậy có đúng không? Cảm ơn anh
Em tạo ra 2 project: 1 project Login, 1 project tạo mã QR
Project Login:
MainWindow.xaml
<Window x:Class="Login.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Login"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel x:Name="stpLogin">
<StackPanel>
<TextBlock Text="Login" HorizontalAlignment="Center" FontSize="20" FontFamily="Times New Roman"></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Text="UserName: " FontSize="20"></TextBlock>
<TextBlock Text=" "></TextBlock>
<TextBox Width="200"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Password: " FontSize="20"></TextBlock>
<TextBlock Text=" "></TextBlock>
<PasswordBox Width="200"></PasswordBox>
</StackPanel>
</StackPanel>
</StackPanel>
</Window>
Project tạo mã QR:
MainWindow.xaml:
<Window x:Class="QRCodeToLogin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:QRCodeToLogin"
xmlns:Duc="clr-namespace:Login;assembly=Login"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<Duc:MainWindow></Duc:MainWindow>
</Window.DataContext>
<StackPanel>
<TextBlock Text="QR Code"></TextBlock>
<Image x:Name="imgQR" Source="{Binding Login}" Width="200" Height="200"></Image>
<TextBox Text="{Binding Login}" x:Name="txtQRCode"></TextBox>
<Button Content="QR Generate" Click="Button_Click"></Button>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace QRCodeToLogin
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
QRCodeGenerator qr = new QRCodeGenerator();
QRCodeData data = qr.CreateQrCode(txtQRCode.Text, QRCodeGenerator.ECCLevel.Q);
QRCode code = new QRCode(data);
Bitmap QRCodeImage = code.GetGraphic(5);
imgQR.Source = BitmapToImageSource(QRCodeImage);
}
private ImageSource BitmapToImageSource(Bitmap Bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
Bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
}
}
chạy thử là biết có đúng ý hay không mà bạn @@