我安装了nuget包 QRCoder
这是打印预览按钮所在页面
<Window x:Class="项目名.QRCodeWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
BorderThickness="0" AllowsTransparency="True" ResizeMode="CanResize" Background="Transparent"
BorderBrush="Transparent" WindowStartupLocation="CenterScreen" WindowStyle="None"
FontSize="28" Width="800" Height="600">
<Border Background="#FFFFFFFF" CornerRadius="6" Margin="5" >
<Border.Effect>
<DropShadowEffect Opacity="1" Direction="0" BlurRadius="10" ShadowDepth="0"/>
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="120" />
</Grid.RowDefinitions>
<Image x:Name="img" Source="{Binding QrCodeImage}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<DockPanel Grid.Row="1" HorizontalAlignment="Center">
<Button Command="{Binding CancelCommand}" Style="{StaticResource GrayButton}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Content="取消" IsCancel="True" />
<Button Content="导出" Style="{StaticResource BlueButton}"
Command="{Binding ReportCommand}"/>
<Button Content="打印预览" Width="280" Style="{StaticResource BlueButton}"
Command="{Binding PrintCommand}" CommandParameter="{Binding ElementName=img}"/>
</DockPanel>
</Grid>
</Border>
</Window>
QRCodeWin.xmal.cs
public partial class QRCodeWin : Window
{
QRCodeViewModel Vm { get; set; }
public QRCodeWin()
{
InitializeComponent();
this.DataContext = Vm = new QRCodeViewModel();
this.Loaded += (s, w) => { Vm.GenerateQRCode(); };
}
public QRCodeWin(bool layout,bool p)
{
InitializeComponent();
this.DataContext = Vm = new QRCodeViewModel(layout,p);
this.Loaded += (s, w) => { Vm.GenerateQRCode(); };
}
}
QRCodeViewModel .cs
public partial class QRCodeViewModel : ObservableObject
{
bool HasLayout;
bool HasProtocol;
public QRCodeViewModel()
{
}
public QRCodeViewModel(bool layout, bool p) : this()
{
HasLayout = layout;
HasProtocol = p;
}
[ObservableProperty]
BitmapImage _QrCodeImage;
[RelayCommand]
private void Print(Image img)
{
new PrintPreviewWin(QrCodeImage).ShowDialog();
}
[RelayCommand]
void Report()
{
try
{
// 创建BitmapFrame
BitmapFrame bitmapFrame = BitmapFrame.Create(QrCodeImage);
// 创建JPEG图像编码器(如果需要导出JPEG格式,请使用new JpegBitmapEncoder())
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(bitmapFrame);
using var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileName = Path.Combine(folderBrowserDialog.SelectedPath, $"{想要取的名称}.png");
// 打开文件流,准备写入
using (var fileStream = new FileStream(fileName, FileMode.Create))
{
// 将编码后的图像写入文件流
encoder.Save(fileStream);
}
}
}
catch (Exception ex)
{
LogHelper.GetSingleObj().WriteLog(ex);
}
}
public void GenerateQRCode()
{
try
{
var sb = new List<byte>();
foreach (var t in 想要传递的名称)
{
string str = t.ToString();
if (Util.IsChinese(str))
{
sb.AddRange(Encoding.GetEncoding("GB2312").GetBytes(str));
}
else
{
sb.AddRange(Encoding.ASCII.GetBytes(str));
}
}
var namelist = sb.Select(x => (int)x).ToList();
List<int> codelist = new List<int>() { namelist.Count };//名称有几个字节
codelist.AddRange(namelist);//名称
//这些是生成二维码代码
using var qrGenerator = new QRCodeGenerator();
// 降低纠错级别以增加容量
using QRCodeData qrCodeData = qrGenerator.CreateQrCode(codelist.Select(x => (byte)x).ToArray(), QRCodeGenerator.ECCLevel.L); // 纠错级别L
using var qrCode = new QRCode(qrCodeData);
using var qrCodeImage = qrCode.GetGraphic(20);
using var memoryStream = new MemoryStream();
qrCodeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
memoryStream.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
// 更新 ViewModel 中的 QrCodeImage 属性
QrCodeImage = bitmapImage;
}
catch (Exception ex)
{
LogHelper.GetSingleObj().WriteLog(ex);
}
}
[RelayCommand]
void Cancel(QRCodeWin win)
{
win?.Close();
}
}